Write a program to swap two variables without using third variable? (two variables a,b. The value of a should go & b to a) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Write a program to swap two variables without using third variable? (two variables a,b. The value of a should go & b to a)

26th Jul 2017, 4:57 AM
Purvesh Jain
Purvesh Jain - avatar
1 Answer
+ 12
a = a + b; b = a - b; a = a - b; In Ruby, you can just do this tho: a, b = b, a There is also the bitshift method, waiting for someone to fill in.
26th Jul 2017, 4:59 AM
Hatsy Rei
Hatsy Rei - avatar