Method1) Using temporary variable
temp=a;
a=b;
b=temp
Method2 ) Using Macro
#define swap(type,a,b) type temp; temp=a; a=b; b=temp;
Method3) Using add/subtract
a=a+b;
b=a-b;
a=a+b;
Method4) Using Multiplication/division
a=a*b;
b=a/b;
a=a/b;
Method5) Using XOR method
a=a^b;
b=a^b;
a=a^b;
Method6) Wrong!! Because evaluation of expression can not be guesses
a=a+b-b=a;

Leave a Reply
You must be logged in to post a comment.