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

Status
Not open for further replies.

Liverpool_fan

Sami Hyypiä, LFC legend
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
Code:
v1, v2 = v2, v1
 

ss max

Broken In
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.
 
Status
Not open for further replies.
Top Bottom