Wednesday, November 21, 2012

swap two numbers without another variable


swap two numbers without another variable

class Swap
{
    public static void main(String[] args)
    {
        System.out.println("Swapping 2 numbers without 3rd variable");
        int a=10;
        int b=20;
        System.out.println("Before swapping a="+a+" and b="+b);
        b=a+b-(a=b);
        System.out.println("After swapping a="+a+" and b="+b);

    }
}


OutPut:


0 comments:

Post a Comment