June 15, 2006

swapping two variable without using temp

This is very trivial stuff. Still I thought it could be helpful to someone.

Let two variables to swap be 'a' and 'b'

so,
step 1) a = a + b
step 2) b = a - b
step 3) a = a - b


2 comments:

Unknown said...

in C in one statement u can do it as

a= a+b - (b=a);

Bhavya Jain said...

was useful thx keep posting :)