Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

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

user-image
Question ajoutée par Muhammad Zahid , IT Project Manager , Cozmuler (PVT) Ltd
Date de publication: 2014/02/11
mohamed hassan
par 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
par 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
par 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

Avez-vous besoin d'aide pour créer un CV ayant les mots-clés recherchés par les employeurs?