Java Program to Swap Two Numbers Without Using Third Variable

Java Program to Swap Two Numbers Without Using Third Variable

public class SwapNumbers {
public static void main(String[] args) {
int a = 10;
int b = 20;

a = a + b;
b = a – b;
a = a – b;

System.out.println(“After swapping:”);
System.out.println(“a = ” + a);
System.out.println(“b = ” + b);
}
}

 

Output:

After swapping:
a = 20
b = 10

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *