Swap Three Variables With or Without Using Temporary Variable C++

In computer programming, the act of swapping three variables using C++ refers to mutually exchanging the values of the variables.  We can swap three variables with or without using a temporary variable in C++.

Swapping Three Variables Using Temporary Variable

Given three variables, a, b and c, swap them with a temporary variable.

Example:

Input  : a = 10, b = 20 and c = 30
Output : a = 30, b = 10 and c = 20
// C++ program to swap three variables 
// with using temporary variable. 
#include <iostream> 
using namespace std; 

// Assign c's value to a, a's value to b and 
// b's value to c. 
void swapThree(int &a, int &b, int &c) 
{ 
	int temp; //variable for storing temporary data
	// give the value of a to temp, temp has value of a, so we can swap any value with a 
	temp = a; // (a = temp = 10) 

	// After this, a has value of c 
	a = c; // (a = c = 30) 

	// After this, c has value of b
	c = b; // (c = b = 20)

	// After this, b has value of a (stores previously in temp)
	b = temp; //(b = temp = 10) 
} 

// Driver code 
int main() 
{ 
	int a = 10, b = 20, c = 30; 

	cout << "Before swapping a = " << a << ", b = "
		<< b << ", c = " << c << endl; 

	swapThree(a, b, c); 

	cout << "After swapping a = " << a << ", b = "
		<< b << ", c = " << c << endl; 

	return 0; 
} 

Output

Before swapping a = 10, b = 20, c = 30
After swapping a = 30, b = 10, c = 20

Method:

The logic is to store variable a, value in temp variable. The numbers can then be swapped with each other. Swapping three variables in C++ using a temporary variable is very easy.

Swapping Three Variables Without Using Temporary Variable

Given three variables, a, b and c, swap them without a temporary variable.

Example

Input  : a = 10, b = 20 and c = 30
Output : a = 30, b = 10 and c = 20
// C++ program to swap three variables 
// without using temporary variable. 
#include <iostream> 
using namespace std; 

// Assign c's value to a, a's value to b and 
// b's value to c. 
void swapThree(int &a, int &b, int &c) 
{ 
	// Store sum of all in a 
	a = a + b + c; // (a = 60) 

	// After this, b has value of a 
	b = a - (b+c); // (b = 60 – (20+30) =10) 

	// After this, c has value of b 
	c = a - (b+c); // (c = 60 – (10 + 30) = 20) 

	// After this, a has value of c 
	a = a - (b+c); //(a = 60 – (10 + 20) = 30) 
} 

// Driver code 
int main() 
{ 
	int a = 10, b = 20, c = 30; 

	cout << "Before swapping a = " << a << ", b = "
		<< b << ", c = " << c << endl; 

	swapThree(a, b, c); 

	cout << "After swapping a = " << a << ", b = "
		<< b << ", c = " << c << endl; 

	return 0; 
} 

Output

Before swapping a = 10, b = 20, c = 30
After swapping a = 30, b = 10, c = 20

Method

The idea is to get a sum in one of the two given numbers. The numbers can then be swapped using the sum and subtraction from the sum.


Mohsin Saeed

MOHSIN SAEED – SEO Researcher & Full Stack Web Developer. https://mohsinsaeed.me That is all you ever need to know about me.

One thought on “Swap Three Variables With or Without Using Temporary Variable C++

Leave a Reply

%d bloggers like this: