How to swap two numbers without using third variable?

How to swap two numbers without using third variable?

 

public class MySwapingTwoNumbers {
    public static void main(String a[]){
        int x = 10;
        int y = 20;
        System.out.println("Before swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
        x = x+y;
        y=x-y;
        x=x-y;
        System.out.println("After swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
    }
}
Output:
Before swap:
x value: 10
y value: 20
After swap:
x value: 20
y value: 10

 

One Reply to “How to swap two numbers without using third variable?”

Leave a Reply

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