+ 1
How to swap two numbers without using 3rd variable..?
Suppose x=2 and y=4. How will you make y=2 and x=4 without using any third variable??
4 Antworten
+ 1
x=x+y
y=x-y
x=x-y
as x=2 and y=4
x=x+y= 2+4=6
y=x-y= 6-4=2
x=x-y= 6-2=4
you can check it on any input
0
y = x + y
x = y - x
y = y - x
0
You can also use XOR.
x = x ^ y;
y = y ^ x;
x = x ^ y;
0
x, y = y, x