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

Encapsulation, Access Specifiers and Scope of Variables in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on encapsulation: private, default, protected and public access, data hiding with getter and setter methods, instance vs static (class) variables, local variables and scope, with a solved BankAccount program.

ICSE Class 9 Computer Applications Sample Paper 2026-27 with Answers
A complete ICSE Class 9 Computer Applications sample paper for the 2026-27 annual exam covering OOP concepts, data types, operators, the Math class, conditional statements and loops, followed by full answers with explanations and solved Java programs.

Wrapper Classes and Character Methods in Java for ICSE Class 10
ICSE Class 10 Computer Applications notes on wrapper classes: primitive types and their wrappers, autoboxing and unboxing, parseInt and valueOf conversions, and Character class methods like isDigit, isLetter and toUpperCase, with output questions and a solved program.