DIGIT-ARC read-only archive · snapshot: July 23, 2026

Swap two numbers without using a temporary variable

25 posts · originally from https://geek.digit.in/community/threads/swap-two-numbers-without-using-a-temporary-variable.85613/

PcEnthu
PcEnthu Original post #1
Apr 17, 2008 at 3:38 PM
Ok guys. Here is the problem. There are two variables x and y which hold numbers. You have to swap the value of the two variables. U might say, just store the value of x in a temporary variable z, store value of y in x and finally store z value in y :D Here is the tricky part, u have to do it without using the variable z
mediator
Apr 17, 2008 at 3:51 PM
x=a
y=b
x=x+y
y=x-y
x=x-y

show x,y
K
Kalyan #3
Apr 17, 2008 at 3:54 PM
x,y be the nos.

1) x=x+y
2) y=x-y
3) x=x-y

u got them swapped.
VINSTAR
VINSTAR #4
Apr 17, 2008 at 3:55 PM
x=x+y
y=x-y
x=x-y
ray|raven
Apr 17, 2008 at 3:58 PM
You can even use division and multiplication operators.

x=x*y
y=x/y
x=x/y
PcEnthu
PcEnthu #6
Apr 17, 2008 at 3:58 PM
@ all u guys, That was fast and u got that cracked that easily. Well done. I will try to come up with some level up questions later.
QwertyManiac
Apr 17, 2008 at 3:59 PM
Or if you wish to do it the l33t way, use XOR.

Starting with x=10, y=20

x^=y
y^=x
x^=y

Now x=20 and y=10.
Logic's the same as addition mostly, except its bitwise. ;)

Edit: This a challenge or a homework question? :confused: For challenges, there exists a thread, I hope you can revive it with better questions than the ones already present.
blueshift
Apr 17, 2008 at 3:59 PM
// 'a' and 'b' are the two variables.

a=a^b
b=a^b
a=a^b
PcEnthu
PcEnthu #9
Apr 17, 2008 at 4:03 PM
You can even use division and multiplication operators.

x=x*y
y=x/y
x=x/y

That would work for positive and negative numbers and not for zero
ray|raven
ray|raven #10
Apr 17, 2008 at 4:12 PM
^Yep. Thats one limitation, by using div.
J
Jove #11
Apr 21, 2008 at 12:20 PM
yeah...XORing is much better as compared to other suggested methods(here).
Other methods may not work if we are dealing with MAX possibles values of respective data types which results in overflow..so,
if(a!=b) ///if a==b, no need of swapping values
a^=b^=a^=b;
swaps the values in a and b

cheers :)
D
Apr 22, 2008 at 10:27 AM
x = 2
y = 4

y = y-x = 2
x = y+y = 4
ray|raven
ray|raven #13
Apr 22, 2008 at 10:32 AM
x = 2
y = 4

x = y-x = 2
y = x+x = 4

Lolz, that works only for those no's i guess.

Try these,
x=5
y=7

x = y-x = 7-5 = 2 ?
y = x+x = 2+2 = 4 ?

Crappy logic.
H
hariharan #14
Apr 22, 2008 at 10:46 AM
i can do that in a single statement.. swap two number in w/o temp var


x ^= y ^= x ^= y;


This swaps the two varaibles... OF course in c and other languages which support bit wise operators..!!
QwertyManiac
Apr 22, 2008 at 10:54 AM
@rayraven - Lol! :lol: By the way, D-Tek.com and Dheeraj are one and the same I think.

x = 2
y = 4

x = y-x = 2
y = x+x = 4
Hahah, moreover you started with x=2 and y=4 and ended with the same :lol:
ray|raven
ray|raven #16
Apr 22, 2008 at 10:59 AM
^Lolz, i missed that part :p
What was he thinking while writing it?

I've known guys who try to deduce logic from examples.
Crappy way, if you ask me. 99% of the time you end up with a logic that works only for the example.
D
Apr 22, 2008 at 2:43 PM
oh sorry i am awake from 24 hour :p

public
class Swap{
staticvoid swap(int x,int y){
//int temp=x;
x^=y; //2
y^=x; // 5
x^=y; //7
System.out.println("After swapping x = " + x + " y = " + y);
}
publicstaticvoid main(String[] args){
int x=5;
int y=7;

System.out.println("Before swapping x="+x+" y="+y);
swap(x,y);

}

}

//one line logic

public class Swap{
static void swap(int x,int y){
y=(x+y)-(x=y);
System.out.println("After swapping x= " + x + " y = " + y);
}
public static void main(String[] args){
int x=5;
int y=7;

System.out.println("Before swapping x="+x+" y="+y);
swap(x,y);

}
}
aditya.shevade
Apr 22, 2008 at 7:07 PM
LOL this has been discussed before....

anther way apart from the XOR way is,

b = a + b - (a = b); you are done.
eggman
eggman #19
Apr 22, 2008 at 10:25 PM
x = 2
y = 4

x = y-x = 2
y = x+x = 4

Ha ha ha ha ha ha ha ha ha ha ha ha ha ha

I've never seen a post funnier and noobier than this!!!!!
aditya.shevade
Apr 28, 2008 at 9:42 PM
Oi mehul.... he said he has not... he did not say you have not....
aditya.shevade
Apr 29, 2008 at 9:11 PM
^^ yeah... I guess :D

Was it Mehul?
Vivek788
Vivek788 #25
Apr 29, 2008 at 11:56 PM
well u can use these in a loop or condition statement like the if or for to avoid a semicolon.