# How to swap two variables in C ++, Python and JavaScript

As a programmer, you’ve probably come across a situation that requires you to swap two numbers. Swapping two numbers is one of the most common situations that programmers face while coding.

You can swap two numbers using a temporary variable or by using arithmetic and bitwise operations. In this article, you will learn about a variety of methods that allow you to exchange two numbers.

How to swap two numbers using a temporary variable

Using a temporary variable is the easiest way to swap two numbers. Follow these three simple steps:

**Step 1**: Assign the value of the 1st variable to a temporary variable.

**2nd step**: Assign the value of the 2nd variable to the 1st variable.

**Step 3**: Assign the value of the temporary variable to the 2nd variable.

For example:

Let num1 = 80 and num2 = 50 (before permutation).

**After step 1**: num1 = 80, num2 = 50 and temp = 80.

**After step 2**: num1 = 50, num2 = 50 and temp = 80.

**After step 3**: num1 = 50, num2 = 80 and temp = 80.

Thus, num1 is equal to 50 and num2 is equal to 80 after permutation.

### C ++ implementation to swap two numbers using a temporary variable

Below is the C ++ implementation to swap two numbers using a temporary variable:

`#include `

using namespace std;

// Function to swap two numbers

// using a temporary variable

void swapNums(int num1, int num2)

{

// Printing numbers before swapping

cout << "Before Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

// Swapping with the help of a

// temporary variable "temp"

int temp = num1;

num1 = num2;

num2 = temp;

// Printing numbers after swapping

cout << "After Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

}

// Driver Code

int main()

{

swapNums(80, 50);

return 0;

}

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

### Python implementation to swap two numbers using a temporary variable

Below is the Python implementation to swap two numbers using a temporary variable:

`# Function to swap two numbers`

# using a temporary variable

def swapNums(num1, num2):

# Printing numbers before swapping

print("Before Swapping:")

print("num1: " , num1 , ", num2: " , num2)

# Swapping with the help of a

# temporary variable "temp"

temp = num1

num1 = num2

num2 = temp

# Printing numbers after swapping

print("After Swapping:")

print("num1: " , num1 , ", num2: " , num2)# Driver Code

swapNums(80, 50)

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

### JavaScript implementation to swap two numbers using a temporary variable

Below is the JavaScript implementation to swap two numbers using a temporary variable:

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

How to swap two numbers using arithmetic operators (addition and subtraction)

First, get the sum of two numbers. Then you can get the required numbers by using the sum and the sum subtraction.

### C ++ implementation to swap two numbers using arithmetic operators (addition and subtraction)

Below is the C ++ implementation for swapping two numbers using arithmetic operators (addition and subtraction):

`#include `

using namespace std;

// Function to swap two numbers

// using arithmetic operators (+, -)

void swapNums(int num1, int num2)

{

// Printing numbers before swapping

cout << "Before Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

// Swapping with the help of

// artithmetic operators (+, -)

num1 = num1 + num2;

num2 = num1 - num2;

num1 = num1 - num2;

// Printing numbers after swapping

cout << "After Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

}

// Driver Code

int main()

{

swapNums(80, 50);

return 0;

}

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

### Python implementation to swap two numbers using arithmetic operators (addition and subtraction)

Below is the Python implementation for swapping two numbers using arithmetic operators (addition and subtraction):

`# Function to swap two numbers`

# using arithmetic operators (+, -)

def swapNums(num1, num2):

# Printing numbers before swapping

print("Before Swapping:")

print("num1: " , num1 , ", num2: " , num2)

# Swapping with the help of

# arithmetic operators (+, -)

num1 = num1 + num2

num2 = num1 - num2

num1 = num1 - num2

# Printing numbers after swapping

print("After Swapping:")

print("num1: " , num1 , ", num2: " , num2)# Driver Code

swapNums(80, 50)

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

### JavaScript implementation to swap two numbers using arithmetic operators (addition and subtraction)

Below is the JavaScript implementation to swap two numbers using arithmetic operators (addition and subtraction):

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

How to swap two numbers using arithmetic operators (multiplication and division)

You can swap two numbers using multiplication and division in three simple steps:

**Step 1**: num1 = num1 * num2

**2nd step**: num2 = num1 / num2

**Step 3**: num1 = num1 / num2

The values of num1 and num2 are swapped.

This is not a preferred method of swapping two numbers because if one of the numbers is 0, the product of those two numbers will also be 0. Also, if the 2nd number is 0, the compilers will throw a division by error. zero. So, you should avoid this approach to swap two numbers.

How to swap two numbers using bitwise operators

The bitwise XOR operator is used to swap two numbers.

### C ++ implementation to swap two numbers using bitwise operators

Below is the C ++ implementation for swapping two numbers using XOR operators:

`#include `

using namespace std;

// Function to swap two numbers

// using XOR operator

void swapNums(int num1, int num2)

{

// Printing numbers before swapping

cout << "Before Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

// Swapping with the help of

// XOR operator

num1 = num1 ^ num2;

num2 = num1 ^ num2;

num1 = num1 ^ num2;

// Printing numbers after swapping

cout << "After Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

}

// Driver Code

int main()

{

swapNums(80, 50);

return 0;

}

Production:

`Before Swapping: `

num1 = 80, num2 = 50

After Swapping:

num1 = 50, num2 = 80

### Python implementation to swap two numbers using bitwise operators

Below is the Python implementation for swapping two numbers using XOR operators:

`# Function to swap two numbers`

# using XOR operator

def swapNums(num1, num2):

# Printing numbers before swapping

print("Before Swapping:")

print("num1: " , num1 , ", num2: " , num2)

# Swapping with the help of

# XOR operator

num1 = num1 ^ num2

num2 = num1 ^ num2

num1 = num1 ^ num2

# Printing numbers after swapping

print("After Swapping:")

print("num1: " , num1 , ", num2: " , num2)# Driver Code

swapNums(80, 50)

Production:

`Before Swapping:`

num1: 80 , num2: 50

After Swapping:

num1: 50 , num2: 80

### JavaScript implementation to swap two numbers using bitwise operators

Below is the JavaScript implementation to swap two numbers using XOR operators:

Production:

`Before Swapping:`

num1: 80, num2: 50

After Swapping:

num1: 50, num2: 80

One line solution to swap two numbers in C ++, Python and JavaScript

You can also swap two numbers on a line without using library functions.

### C ++ implementation for a single line solution

`#include `

using namespace std;

int main()

{

int num1 = 80, num2 = 50;

cout << "Before Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

// One line solution to swap two numbers

num1 = num1 ^ num2, num2 = num1 ^ num2, num1 = num1 ^ num2;

cout << "After Swapping: " << endl;

cout << "num1 = " << num1 << ", num2 = " << num2 << endl;

return 0;

}

Production:

`Before Swapping:`

num1: 80, num2: 50

After Swapping:

num1: 50, num2: 80

### Python implementation for a one-line solution

`num1 = 80`

num2 = 50

print("Before Swapping:")

print("num1: " , num1 , ", num2: " , num2)

# One line solution to swap two numbers

num1, num2 = num2, num1

print("After Swapping:")

print("num1: " , num1 , ", num2: " , num2)

Production:

`Before Swapping:`

num1: 80, num2: 50

After Swapping:

num1: 50, num2: 80

### JavaScript implementation for a one-line solution

Production:

`Before Swapping:`

num1: 80, num2: 50

After Swapping:

num1: 50, num2: 80

If you want to check out the full source code used in this article, here is the GitHub repository.

Improve your programming habits

If you want to improve your programming habits, you should follow some programming principles like KISS (Keep It Simple, Stupid), Dry Code, YAGNI (You Aren’t Going to Need It), etc. coding errors, you need to know the most common coding errors. Knowledge will help you avoid common pitfalls and keep your code meaningful.

Read more

**About the Author**