How to swap two variables without extra space

Let’s say, we have two variables, A and B, and our task is swapping these two variables without extra space.

First, we can this:

A = A + B

and,

B = A – B = A + B – B = A (import A from Step 1)

finally,

A = A – B = A + B – A = B (import A, B from Step 1 and 2 respectively)

That’s it, Dude!

Leave a Reply

Your email address will not be published. Required fields are marked *