Friday, September 12, 2014

Swap two number without using temp variable

Code:
        int X = 5;
        int Y = 6;
        System.out.println("Before swap X is : "+X+", Y is : "+Y);
        Y = X+Y;
        X = Y-X;
        Y = Y-X;
        System.out.println("After swap X is : "+X+", Y is : "+Y);

Output: 

Before swap X is : 5, Y is : 6
After swap X is : 6, Y is : 5

No comments:

Post a Comment