## Swap values of three variables without using fourth variable

February 15, 2007 by chiragrdarji

In my recent interview, a well known company in ahmedabad having branches in all over India, I was asked to write a programm. I have to swap the values of three variables without using fourth varable. I found it really triky. I will first explain this with two variables and than show for three variables in two diffrent ways.

Lets, consider the case a = 10 and b=20. I have to write the programm so that result will be a= 20 and b= 10. Below is the steps,

a = 10;

b=20;

a = a+b; ( a= 30)

b = a – b; (b = 30 – 20 =10)

a = a-b; (a = 30-10 = 20)

Fig – 1 Steps to swap the values of two variables.

Now take three variables, a= 10, b=20 and c =30. The output should be a=30, b=10 and c= 20. First way to achive this is you can use steps mentioned in Fig -1 three times. First swap a and b than a and c. Below is the steps,

a = 10;

b=20;

c=30;

a = a+b; ( a= 30)

b = a – b; (b = 30 – 20 =10)

a = a-b; (a = 30-10 = 20)

// Here a= 20 and b= 10;

a = a+c; ( a= 20 + 30 = 50)

c = a – c; (c = 50 – 30 =20)

a = a-c; (a = 50-20 = 30)

// Here a= 30 and c= 20;

Fig – 2 Steps to swap the values of three variables.

Second way of swapping the values of three variable is,

a = 10;

b=20;

c=30;

a = a+b+c; ( a= 60)

b = a – (b+c); (b = 60 – (20+30) =10)

c = a- (b+c); (c = 60 – (10 + 30) = 20)

a = a- (b+c); (a = 60 – (10 + 20) = 30)

Fig – 3 Steps to swap the values of three variables.

Happy Programing.

### Like this:

Like Loading...

*Related*

on October 24, 2007 at 1:03 pm |karthickthank u

on October 22, 2008 at 10:19 am |GaneshBefore i was confused with this qestioned…and now

i got cleared my doubt….

Thnx Buddy

on July 14, 2009 at 7:52 pm |SeamusThat’s pretty neat.

You can improve this technique by using the “exclusive or” (XOR) bitwise operation. This enables you to swap variables without regard for datatype.

For two variables:

a = a XOR b

b = a XOR b

a = a XOR b

For three variables:

a = a XOR b XOR c

b = a XOR b XOR c

c = a XOR b XOR c

a = a XOR b XOR c

For n variables:

v1 = v1 XOR v2 XOR v3 … XOR vn

v2 = v1 XOR v2 XOR v3 … XOR vn

v3 = v1 XOR v2 XOR v3 … XOR vn

…

vn = v1 XOR v2 XOR v3 … XOR vn

v1 = v1 XOR v2 XOR v3 … XOR vn

-Seamus

on April 24, 2011 at 6:02 pm |SabarishFantastic……..

on September 16, 2011 at 7:18 pm |Adrija MukhopadhyayThanks a lot….but can u tell me how can I Swap values of three variables without using fourth variable…

on February 5, 2012 at 2:50 am |Thamarai SelvanThe third way to solve the problem:

a=10;

b=20;

c=30;

a = a*b*c; ( a= 6000)

b = a / (b*c); (b = 6000 / (20*30) =10)

c = a / (b*c); (c = 6000 / (10 * 30) = 20)

a = a / (b*c); (a = 60 / (10 * 20) = 30)

on November 14, 2015 at 4:19 pm |Mr. CrackerGreat Job.

on February 5, 2012 at 2:52 am |Thamarai SelvanThe third way to solve the problem:

a=10;

b=20;

c=30;

a = a*b*c; ( a= 6000)

b = a / (b*c); (b = 6000 / (20*30) =10)

c = a / (b*c); (c = 6000 / (10 * 30) = 20)

a = a / (b*c); (a = 6000 / (10 * 20) = 30)

on July 16, 2012 at 12:48 pm |Anandsuper

on January 12, 2013 at 5:23 pm |santropedroYou can solve the general case of swapping n variables with the next algorithm:

a1=a1+a2+a3…an

a2=a1-a2-a3…-an

a3=a1-a2-a3…-an

a4=a1-a2-a3…-an

….

an=a1-a2-a3…-an

a1=a1-a2-a3…-an

using exactly (n+1) assignments and that’s the minimum number of assignments.

And the variables are swapping like this: a1 values jumps to a2, a2 value to a3, and an value to a1.

on March 12, 2016 at 5:08 am |sai kumar reddythank u, i got much knowledge after reading this post

on June 13, 2016 at 5:35 pm |boost brain powerYou actually suggested that superbly.

on May 11, 2017 at 7:50 am |events recruitmentCompetency, professionalism, reliability , adherence to best recruitment practices are some with the characteristic top features of international search firms that really help

them in delivering ideal recruitment services on their

clients in various countries. For getting recruited inside Military

engineering Services, candidates must have passed the engineering degrees.

Since that time, SBI continues to be very active and may be serving as a custodian of people’s money inside form of various bank accounts.