# Swap Two Numbers

Swap Two Numbers” is a basic programming exercise for beginners. Here, we are given two numbers, entered by user and our task is to write a program to swap these numbers.

Example (swap two numbers):

```Before Swapping:
A = 10, B = 20
After Swapping
A = 20, B = 10```

There are basically two ways to swap two numbers. These are:

1. With using extra variable
2. Without using extra variable

The time complexity of both the solutions is O(1).

METHOD 1: Swap Two Numbers with extra variable

To swap two numbers by using extra variable, we will copy the first number to extra variable. Then, copy the second number to first variable. Lastly, copy the extra variable number to second number.

C++ Program to swap two numbers with using extra variable is as follows:

```/* C++ Program to swap two numbers */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num1,num2;

//Scan the number
cout<<"Enter the numbers:";
cin>>num1>>num2;

//Numbers Before swapping
cout<<"Numbers Before Swapping”<<endl;

cout<<"NUM1: "<<num1<<endl;
cout<<"NUM2: "<<num2<<endl;

int temp;
// copy the first number to temp variable
temp = num1;
// copy the second number to first number
num1 = num2;
// copy the temp variable to first number
num2 = temp;

//Numbers After swapping
cout<<"Numbers After Swapping”<<endl;
cout<<"NUM1: "<<num1<<endl;
cout<<"NUM2: "<<num2<<endl;
}
```
```OUTPUT:
Enter the numbers: 21 31
Numbers Before Swapping
NUM1: 21
NUM2: 31
Numbers After Swapping
NUM1: 31
NUM2: 21```

METHOD 2: Swap Two Numbers without extra variable

Arithmetic or bitwise XOR operators can be to swap two number without using third variable.

1. Using Arithmetic Operator

#### C++ Program to swap two numbers using arithmetic operator is as follows:

```/* C++ Program to swap two numbers */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num1,num2;

//Scan the number
cout<<"Enter the numbers:";
cin>>num1>>num2;

//Numbers Before swapping
cout<<"Numbers Before Swapping\n";
cout<<"NUM1: "<<num1<<endl;
cout<<"NUM2: "<<num2<<endl;

num1 = num1 + num2;
num2 = num1 - num2;
num1 = num1 - num2;

//Numbers After swapping
cout<<"Numbers After Swapping\n";
cout<<"NUM1: "<<num1<<endl;
cout<<"NUM2: "<<num2<<endl;
}
```
```OUTPUT:
Enter the numbers: 21 31
Numbers Before Swapping
NUM1: 21
NUM2: 31
Numbers After Swapping
NUM1: 31
NUM2: 21```

#### C++ Program to swap two numbers using Bitwise XOR operator is as follows:

```/* C++ Program to swap two numbers */
#include<bits/stdc++.h>
using namespace std;
int main()
{
int num1,num2;

//Scan the number
cout<<"Enter the numbers:";
cin>>num1>>num2;

//Numbers Before swapping
cout<<"Numbers Before Swapping\n";
cout<<"NUM1: "<<num1<<endl;
cout<<"NUM2: "<<num2<<endl;

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

//Numbers After swapping
cout<<"Numbers After Swapping\n";
cout<<"NUM1: "<<num1<<endl;
cout<<"NUM2: "<<num2<<endl;
}

```
```OUTPUT:
Enter the numbers: 21 31
Numbers Before Swapping
NUM1: 21
NUM2: 31
Numbers After Swapping
NUM1: 31
NUM2: 21```

Related Posts: