C++ Program to Swap Two Numbers

Write a C++ Program to Swap Two Numbers using Temporary variables, Bitwise Operators, Arithmetic Operators, Functions, Pointers, and Call by Reference with an example. We explain multiple ways to swap.

C++ Program to Swap Two Numbers using temp

In this example, we are using a temp variable. It allows us to enter the a and b values. Then, it uses the temp variable to interchange those two values.

#include<iostream>
using namespace std;

int main()
{
	int a, b, temp;
	
	cout << "\nPlease Enter the First Number : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second Number : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before Swapping: a = "<< a << " and b = " << b;
	
	temp = a;
	a = b;
	b = temp;
	
	cout << "\nThe Result After Swapping : a = "<< a << " and b = " << b;

 	return 0;
}
C++ Program to Swap Two Numbers 1

C++ Program to Swap Two Numbers using the Bitwise Operators

#include<iostream>
using namespace std;

int main()
{
	int a, b;
	
	cout << "\nPlease Enter the First Value : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second Value : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before : a = "<< a << " and b = " << b;
	
	a = a ^ b;
	b = a ^ b; 
	a = a ^ b;
	
	cout << "\nThe Result After : a = "<< a << " and b = " << b;

 	return 0;
}
Please Enter the First Value : a =  30

Please Enter the Second Value : b =  40

The Values Before : a = 30 and b = 40
The Result After : a = 40 and b = 30

This Program helps to Swap Two Numbers using the Arithmetic Operators.

#include<iostream>
using namespace std;

int main()
{
	int a, b;
	
	cout << "\nPlease Enter the First : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before : a = "<< a << " and b = " << b;
	
	a = a + b;
	b = a - b; 
	a = a - b;
	
	cout << "\nThe Result After : a = "<< a << " and b = " << b;

 	return 0;
}
Please Enter the First : a =  9

Please Enter the Second : b =  17

The Values Before : a = 9 and b = 17
The Result After : a = 17 and b = 9

Swap Two Numbers using Functions

#include<iostream>
using namespace std;

void swapTwoNumbers(int a, int b)
{
	int temp;
	
	temp = a;
	a = b;
	b = temp;
	
	cout << "\nThe Result After : a = "<< a << " and b = " << b;
}
int main()
{
	int a, b;
	
	cout << "\nPlease Enter the First Num : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second Num : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before : a = "<< a << " and b = " << b;
	
	swapTwoNumbers(a, b);

 	return 0;
}
Please Enter the First Num : a =  222

Please Enter the Second Num : b =  999

The Values Before : a = 222 and b = 999
The Result After : a = 999 and b = 222

This Program Swap Two Numbers using pointers.

#include<iostream>
using namespace std;

int main()
{
	int a, b, *i, *j, temp;
	
	cout << "\nPlease Enter the First Num : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second Num : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before : a = "<< a << " and b = " << b;
	i = &a;
	j = &b;
	
	temp = *i;
	*i = *j;
	*j = temp;
	
	cout << "\n\nThe Result After : a = "<< a << " and b = " << b;
	cout << "\nThe Result After : *i = "<< *i << " and *j = " << *j;
	cout << "\nThe Result After : i = "<< i << " and j = " << j;

 	return 0;
}
Please Enter the First Num : a =  1212

Please Enter the Second Num : b =  8796

The Values Before : a = 1212 and b = 8796

The Result After : a = 8796 and b = 1212
The Result After : *i = 8796 and *j = 1212
The Result After : i = 0x7ffeefbff458 and j = 0x7ffeefbff454

Write a swap two numbers Program using Call by Reference.

#include<iostream>
using namespace std;

void swapTwoNumbers(int *i, int *j)
{
	int temp;
	
	temp = *i;
	*i = *j;
	*j = temp;
	
	cout << "\n\nThe Result After : i = "<< i << " and j = " << j;
	cout << "\nThe Result After  : *i = "<< *i << " and *j = " << *j;
}

int main()
{
	int a, b, *i, *j, temp;
	
	cout << "\nPlease Enter the First : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before : a = "<< a << " and b = " << b;
	i = &a;
	j = &b;
	
	swapTwoNumbers(i, j);	
	cout << "\nThe Result After  : a = "<< a << " and b = " << b;

 	return 0;
}
Please Enter the First : a =  1500

Please Enter the Second : b =  9877

The Values Before : a = 1500 and b = 9877

The Result After : i = 0x7ffeefbff458 and j = 0x7ffeefbff454
The Result After : *i = 9877 and *j = 1500
The Result After : a = 9877 and b = 1500

About Suresh

Suresh is the founder of TutorialGateway and a freelance software developer. He specialized in Designing and Developing Windows and Web applications. The experience he gained in Programming and BI integration, and reporting tools translates into this blog. You can find him on Facebook or Twitter.