# 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 the numbers.

**Example:**

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

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

- With using extra variable
- Without using extra variable

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

**METHOD 1: 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 number with using extra variable is as follows:**

#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; }

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

**METHOD 2: Without extra variable**

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

**Using Arithmetic Operator**

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

#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; }

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

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

#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; }

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

**Related Posts:**

**Program to print Fibonacci Series up to Nth Term.****Program to find and print nth Fibonacci number.****Program to find Power of the Number.****Program to find Quotient and Remainder.****Program to find largest amongst three numbers.****Program to find factorial of a number.****Program to find GCD of two numbers.****Program to find LCM of two numbers.****Program to check whether entered number is odd or even.****Program to check whether entered number is prime number or not.****Program to check whether entered number is palindrome or not.****Program to check whether entered number is Armstrong Number or Not.****Program to convert binary number to octal number.****Program to convert binary number to decimal number.****Program to convert binary number to hexadecimal number.****Program to convert octal number to binary number.****Program to convert octal number to decimal number.****Program to convert octal number to hexadecimal number.****Program to convert decimal number to binary number.****Program to convert decimal number to octal number.**