Switch 2 variable without using temp variable | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Switch 2 variable without using temp variable

It is basic knowledge of algoritm, how to switch the value of 2 integer variable. for example : a = 9 b = 3 switch the value of this two variable so the result is : a = 3 b = 9 without using the third (as temp) variable.

15th Mar 2018, 2:26 PM
Yoshi Mochamad Lutfi
Yoshi Mochamad Lutfi - avatar
4 Answers
+ 7
java,c++,javascript a=a+b; b=a-b; a=a-b;
15th Mar 2018, 3:32 PM
cHiRaG GhOsH
cHiRaG GhOsH - avatar
+ 3
In Python: a, b = b, a :)
15th Mar 2018, 2:58 PM
Just A Rather Ridiculously Long Username
+ 2
>>> a = 3 >>> b = 9 >>> a, b = b, a >>> print (a) 9 >>> print(b) 3 >>>
15th Mar 2018, 2:33 PM
strawdog
strawdog - avatar
+ 1
c++ a=a+b; b=a-b; a=a-b;
15th Mar 2018, 4:31 PM
Amrik Singh