C++ Program to Swap Two Numbers

Write a C++ Program to Swap Two Numbers using a temp variable. It allows us to enter a and b values. Then, it uses the temp variable to swap those two numbers.

#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 Number : a =  ";
	cin >> a;
	
	cout << "\nPlease Enter the Second Number : b =  ";
	cin >> b;
	
	cout << "\nThe Values Before Swapping: a = "<< a << " and b = " << b;
	
	a = a ^ b;
	b = a ^ b; 
	a = a ^ b;
	
	cout << "\nThe Result After Swapping : a = "<< a << " and b = " << b;

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

Please Enter the Second Number : b =  40

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

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

#include<iostream>
using namespace std;

int main()
{
	int a, b;
	
	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;
	
	a = a + b;
	b = a - b; 
	a = a - b;
	
	cout << "\nThe Result After Swapping : a = "<< a << " and b = " << b;

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

Please Enter the Second Number : b =  17

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

C++ Program to 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 Swapping : a = "<< a << " and b = " << b;
}
int main()
{
	int a, b;
	
	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;
	
	swapTwoNumbers(a, b);

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

Please Enter the Second Number : b =  999

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

C++ Program to Swap Two Numbers using pointers

#include<iostream>
using namespace std;

int main()
{
	int a, b, *i, *j, 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;
	i = &a;
	j = &b;
	
	temp = *i;
	*i = *j;
	*j = temp;
	
	cout << "\n\nThe Result After Swapping : a = "<< a << " and b = " << b;
	cout << "\nThe Result After Swapping : *i = "<< *i << " and *j = " << *j;
	cout << "\nThe Result After Swapping : i = "<< i << " and j = " << j;

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

Please Enter the Second Number : b =  8796

The Values Before Swapping: a = 1212 and b = 8796

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

C++ Program to Swap Two Numbers 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 Swapping : i = "<< i << " and j = " << j;
	cout << "\nThe Result After Swapping : *i = "<< *i << " and *j = " << *j;
}

int main()
{
	int a, b, *i, *j, 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;
	i = &a;
	j = &b;
	
	swapTwoNumbers(i, j);	
	cout << "\nThe Result After Swapping : a = "<< a << " and b = " << b;

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

Please Enter the Second Number : b =  9877

The Values Before Swapping: a = 1500 and b = 9877

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