Wednesday, June 29, 2011

swapping of two numbers without using temporary variable

swapping of two numbers using temporary variable :

                              int t;
                              t=a;
                              a=b;
                              b=t;
now consider swapping of two numbers without using the temporary variable:
                               a=a+b;
                               b=a-b;
                               a=a-b;

ex : consider a=10 and b=20
     a=10+20=30
     b=30-20=10
     a=30-10=20
now a=20 and b=10 after swapping

No comments:

Post a Comment