We can do it by single statement

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

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

We can do it by single statement

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

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

The best way to swap between two numbers is by using the basic concepts of mathematics,nothing more...it's so easy!! Consider a=10,b=5. Now take , a=a+b (i.e.10+5=15) b=a-b; (i.e.15-5=10) a=a-b (i.e. 15-10=5) That's so simple...

Swapping two numbers(integers) without using third variable:

A=a^b;

b=a^b;

a=a^b;

A=a^b;

b=a^b;

a=a^b;

All the above may cause overflow.

The one line answer which is full proff is this:

A=a+b-(b=a)

The one line answer which is full proff is this:

A=a+b-(b=a)

suppose we have a=3 n b=2

a= a+b (a=5)

b=a-b (b=3)

a=a-b (a=2)

a= a+b (a=5)

b=a-b (b=3)

a=a-b (a=2)

We can do it by using X-OR

a^=b^=a^=b

(operate it from right)

Nagarjuna Adimulam

a^=b^=a^=b

(operate it from right)

Nagarjuna Adimulam

Didn't find the answer you were looking for?