Swapping numbers program
Write a program to swap two numbers using a temporary variable.
Write a program to swap two numbers using a temporary variable.
public class swap1
{
public static void main()
{
int temp;
int a=3;
int b=9;
temp=a;
a=b;
b=temp;
System.out.println("After swapping, a="+a);
System.out.println("After swapping, b="+b);
}<tbody>
After swapping,a will be=9
After swapping,b will be=3
</tbody>
Write a program to swap two numbers without using a temporary variable.
public class swap2
{
public static void main()
{
int a=3;
int b=9;
a=a+b;
b=a-b;
a=a-b;
System.out.println("After swapping, a="+a);
System.out.println("After swapping, b="+b);
}<tbody>
After swapping,a will be=9
After swapping,b will be=3
</tbody>
rite a program
Related Posts
Continue learning with these related articles

ICSE Class 9 Computer Applications Notes: Values, Data Types and Operators in Java
Easy-to-revise ICSE Class 9 Computer Applications notes on Java tokens, primitive data types, type conversion, and arithmetic, relational, logical, increment and ternary operators, with solved output questions and practice exercises.

ICSE Class 10 Computer Applications Sample Paper 2027 with Solutions
A full-length ICSE Class 10 Computer Applications sample paper for the 2027 board exam in the latest CISCE format: 20 MCQs, 10 short-answer questions and 6 Java programs, followed by complete solutions with explanations and solved BlueJ programs.
ICSE computer applications sample paper
ICSE computer applications sample paper (Set 3): ICSE Class 10 Computer Applications sample paper with important questions for computer exam practice.