Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How do you swap two integer values from two variables without using another variable?

user-image
Question added by Muhammad Zahid , IT Project Manager , Cozmuler (PVT) Ltd
Date Posted: 2014/02/11
mohamed hassan
by mohamed hassan , Senior Java Developer , IST Networks

let us say the two variables are x & y 

 

x =5 

y =7 

 

now we want y =5 and x =7

 

x = x+y // x =12, y=7

y = x-y // x=12 ,y =5

x = x-y// x =7 and y =5 #

Dima Gupta
by Dima Gupta , Sales Champion , VoliveSolutions

lets say a =10, b =20

 

a = a ^ b

 

b = a ^ b

 

a = a ^ b

 

a =20, b =10

Asif Ali M
by Asif Ali M , Project Lead , LiquidHub

The other logic is here

x = x*y

y = x/y

x = x/y

Apply the formula for x=5 and y=6

x =5*6 =30

y =30/6 =5

x =30/5 =6

now x=6 & y=5

 

 

More Questions Like This