Saturday, 23 February 2013

Swap two numbers

Here a,b are two variables of type int.

Method 1:
                          a=a+b;
                          b=a-b;
                          a=a-b;

Method 2:
                          a=a*b;
                          b=a/b;
                          a=a/b;

Method 3:
                          a=a^b;
                          b=a^b;
                          a=a^b;

Method 4:
                          a=(a+b)-(b=a);


Method 5:
                 /*With temp. variable*/
                          temp=a;
                          a=b;
                          b=temp;


                         
                         
                     

No comments:

Post a Comment