+ 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.
4 Antworten
+ 7
java,c++,javascript
a=a+b;
b=a-b;
a=a-b;
+ 3
In Python: a, b = b, a :)
+ 2
>>> a = 3
>>> b = 9
>>> a, b = b, a
>>> print (a)
9
>>> print(b)
3
>>>
+ 1
c++
a=a+b;
b=a-b;
a=a-b;