Conventionally in order to swap two numbers, we use the third variable temp. Swapping two variables is quite comfortable using a temporary variable.
What if the interviewer asks you to swap the numbers without any extra space. Swapping two numbers without using a temporary variable makes the thing interesting.
If you want to swap any two numbers, there are ways to swap the integer or float values by using some sort of arithmetic and bit manipulation operation.
There are three ways for swapping numbers using…
It looks messy at first, but when you look into these, it’s a just simple pattern to remember.
x = x + y y = x - y x = x - y
Addition and subtraction are arithmetic operations. This method can apply for swapping integer as well as floating type values.
#include<stdio.h>; int main() { // intialise two variables int x = 25, y = 17; // Arithmatic Code to swap 'x' and 'y' x = x + y; // after addition x with contain 42 y = x - y; // y will contanis 25 x = x - y; // x becomes 17 printf("After Swapping two values: x = %d, y = %d", x, y); return 0; }
Output:
After Swapping two values: x = 17, y = 25
This method is easy to understand as like arithmetic addition and subtraction methods.
x = x * y y = x / y x = x / y
#include<stdio.h>; int main() { // intialise two variables int x = 25, y = 17; // Arithmatic multiplicatio and division Code to swap 'x' and 'y' x = x * y; // after addition x with contain 42 y = x / y; // y will contanis 25 x = x / y; // x becomes 17 printf("After Swapping two numbers: x = %d, y = %d", x, y); return 0; }
It is similar to the first method. The only difference is that it uses multiplication operation instead of addition and division operation instead of subtraction.
Output:
After Swapping two numbers: x = 17, y = 25
Note: This method will not work in case any of the numbers (x or y) are zero.
x = x ^ y y = x ^ y x = x ^ y
Note: If you are swapping two values using the XOR bit operation, it works for integers only.
C Code:
#include<stdio.h>; int main() { // intialise two variables int x = 25, y = 17; // XOR Bit operation Code to swap 'x' and 'y' x = x ^ y; // after addition x with contain 42 y = x ^ y; // y will contanis 25 x = x ^ y; // x becomes 17 printf("After Swapping two values: x = %d, y = %d", x, y); return 0; }
Output:
After Swapping two values: x = 17, y = 25
C++ Code:
Write a C++ program to swap two numbers without using extra space.
#include <iostream> using namespace std; int main() { int a= 13; int b=5; cout<< a << " "<<b<<endl; a = a^b; b = a^b; a = a^b; cout<<a<<" "<<b; return 0; }
This code snippet is shared by Om Tayade.
The XOR bit operation method is easy to remember as it uses a single operation.
I have described all these methods by writing code in C and C++. You can use all the above three methods to write a program in any language to swap two numbers without using a temporary variable.
If you are a Python developer, you don’t need to do this as well. You can write a Python program to swap two variables without using a third variable by a just single line of code.
Do you know any other method to swap two variables? You can write in the comment section below. I would like to mention that in this post as well.
In the multiplication method, it should be mentioned that x and y should not be equal to zero.
Good catch. Added note in the tutorial. Thanks!