## Develop Your Creative Skill with Free Design Tutorials

Our blog helps to Provide Latest Tips and Tricks tutorials about Blogging
Business, SEO Tips, much more. this Blog and stay tuned to this

# Various ways to swap two numbers

In this Example you will learn to swap two variables by using various amazing technique in any programming language. Swapping means interchanging the values of two entities. Swapping is mutually exchanging values of two variables.

Let us Understand it by using an Example

Input : x = 5, y = 7;
Output : x = 7, y = 5

Input : x = 23, y = 10
Output : x = 10, y = 23

Let us talk some methods to swap two variables

#### Method 1 : Swap two number by using third variable

We can easily Swap two numbers by using a third temporary variable.
Let us say if we want to swap two variable p and q
The idea is simple

Assign p to a temp variable : temp = p
Assign y to p : p = y
Assign temp to q : q = temp

Let us see the brute force code of above method

// Let us say p=6 and q=8

int temp=p;
p=q;
q=temp;

#### Method 2 : Using Arithmetic Operators

Two variables can be swapped by using Arithmetic operator i.e. +,-.
The idea is to get sum of two numbers in one variable and then swap is using + and - operator.

// Code to swap 'X' and 'Y'
x = x + y;
y = x - y;
x = x - y;

print("X -> ", x + "y->",y)

we can also swap two numbers by using multiplication and division operators.
In this methods we will get multiplication of two numbers in one variable and get two variable swapped using multiplication and division operator.

// Let  x = 7, y = 12;
// Let us swap 'x' & 'y'

x = x * y; // x=84
y = x / y; // y=7
x = x / y; // x=12
print("Value Of X and Y ", x, y);

Problem with this method is if you increase the numbers then it may lead to generate overflow error because multiplying two large numbers may overflow from range of integer.

#### Method 2 : Using Bitwise XOR

We can swap two numbers by using bitwise XOR operator. you need to understand how XOR operators works on bits. Here is the Truth table for XOR operation for better understanding of this method.

Here is the code to swap two numbers using XOR operator

// Let  x = 7, y = 12;
// Let us swap 'x' & 'y'

x = x ^ y; // x=15
y = x ^ y; // y=7
x = x ^ y; // x=12
print("Value Of X and Y ", x, y);

this is one of most asked approach in technical interviews. Even we can swap two numbers by using other bitwise operators such as AND and OR operator.

#### Method 3 : Swap two Numbers using One line of Code

You can swap two numbers even by using one one line of code. Although you can swap two numbers by using STL function in C++. but we can easily swap two numbers by using assignment operator within one line.

// Let  x = 7, y = 12;
// Let us swap 'x' & 'y'

x = (x * y) / (y = x);

print("Value Of X and Y ", x, y);

In C++ we can use swap function that takes two argument and swap corresponding two numbers. But in technical interviews you should be aware about others approach also.
If we talk about Time Complexity of above all Methods the they all can be performed in Constant time.

Time Complexity: O(1)

Auxiliary Space: O(1)