Quantcast
Channel: How to swap two variables without using a third variable? - Stack Overflow
Browsing all 6 articles
Browse latest View live

Answer by Krutarth Gor for How to swap two variables without using a third...

You can directly try for:a, b = b, aIt is a canonical way to exchange the value without using a third variable.

View Article



Answer by Gokul Kurup for How to swap two variables without using a third...

This is the main snippet of code: x = x + y; // x now becomes 15 y = x - y; // y becomes 10 x = x - y; // x becomes 5This is what you friend meant.

View Article

Answer by afeldspar for How to swap two variables without using a third...

If your friend is a "math-snob", he may have in mind a particular trick, which you can use in languages where you can apply the XOR function to the bitstring representation of numbers. Say we have...

View Article

Answer by Sylvain Leroux for How to swap two variables without using a third...

The canonical way to swap two variables in Python isa, b = b, aPlease note than this is valid whatever the "type" of a or b is (numeric, string, tuple, object, ...). Of course, it works too if both...

View Article

Answer by todd_dsm for How to swap two variables without using a third variable?

A seemingly simple question. In retrospect, presumably designed to determine whether or not you think mathematically. I pondered, it's not a simple problem but not out of reach.As research reveals this...

View Article


How to swap two variables without using a third variable?

Can you swap the values of two variables without using a third variable as a temporary placeholder?

View Article
Browsing all 6 articles
Browse latest View live




Latest Images