# Is it possible to swap two numbers without using a third temporary variable?



## DIPANSHU_ASRI (Aug 5, 2011)

please answer if u can.


----------



## krishnandu.sarkar (Aug 5, 2011)

Yes

10chars


----------



## nims11 (Aug 5, 2011)

let x and y be two numbers that have some value.
x = x+y
y=x-y
x=x-y.


----------



## mitraark (Aug 5, 2011)

Don;t you have a book or something ? You had to create a thread /?


----------



## arpanmukherjee1 (Aug 5, 2011)

```
a= a^b;
b = b^a;
a = a^b;
```


----------



## Liverpool_fan (Aug 6, 2011)

First of all mention the programming language you are using.

If it's C or C++, above replies will do. Note don't try the one line XOR technique, that is undefined and not portable.

If it's Python

```
v1, v2 = v2, v1
```


----------



## Omi (Aug 7, 2011)

Use pointers


----------



## nims11 (Aug 7, 2011)

Omi said:


> Use pointers



i think it would still require another variable unless the address of two variables are consecutive.


----------



## ss max (Aug 7, 2011)

nims11 said:


> i think it would still require another variable unless the address of two variables are consecutive.


Yes it would require another variable because as soon as you will apply swap on pointer variable the previous value will be gone and you can not do both swaps in single operation.
For swapping follow the approach that nims11 provided.


----------



## Liverpool_fan (Aug 7, 2011)

Think I should assume the  OP is using C/C++ and in that case the thread has run its course.


----------

