Write the logic to swap two numbers in a single statement not in python. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 6

Write the logic to swap two numbers in a single statement not in python.

A single line statement that can swap the values of two variables. Not the case in python.

5th Oct 2018, 5:03 PM
Prateek Jena
Prateek Jena - avatar
10 Answers
+ 12
b=a+b-(a=b)
5th Oct 2018, 5:38 PM
Prateek Jena
Prateek Jena - avatar
+ 6
Nice but you should post it on your Post Feed.
5th Oct 2018, 7:56 PM
blACk sh4d0w
blACk sh4d0w - avatar
+ 4
Cool Unfortunately you code contains undefined behavior and could provide wrong results with other compilers. For example a+=b-(b=a) doesn't work while a+=(b)-(b=a) works in playground.
5th Oct 2018, 7:59 PM
Sergey Ushakov
Sergey Ushakov - avatar
+ 3
[b, a] = [a , b]
5th Oct 2018, 8:37 PM
Sergei No
Sergei No - avatar
+ 2
a,b=b,a
15th Oct 2018, 1:55 PM
Samhita Joshi
Samhita Joshi - avatar
+ 1
a=a+b b=a-b a=a-b
6th Oct 2018, 1:19 PM
Sunil Kumar
Sunil Kumar - avatar
0
b=a+b(a=b)
18th Oct 2018, 3:46 PM
Nahom Urge
Nahom Urge - avatar
0
Nahom you forgot the minus sign.
18th Oct 2018, 4:55 PM
Prateek Jena
Prateek Jena - avatar
0
b=a+(a=b)*0 Could it work?
27th Mar 2019, 4:03 PM
No Borders
No Borders - avatar