Friday, January 23, 2009

Swap X and Y...

I have been asked this programming question more times than I can remember now.

Question:

Given two variables x and y, how would you swap their values without using a third variable?

Think...

Hmm...

Umm...

Ohhh...

Answer:
------------------
x = x + y;
y = x - y;
x = x - y;
------------------

That's it... ^__^

No comments:

Post a Comment