Updated March 17, 2023
Introduction of Swapping in C++
Swapping is nothing but an exchanging data between variables. Like any other language, we can also perform swapping operations. It is performed using two methods – using the third variable and without using the third variable. In this article, we are going to discuss these two methods to swap numbers with the help of examples. To understand swap concept lets discussed one example – suppose you have 500 notes and you need an exchange of 500 rupees. You asked your friend for the 500 exchange and he gives you 5 notes of 100 in return 500 note. Here, in this case, you and your friend just exchange the notes. This is what called a swapping exchanging data between two variables.
How Swapping Works in the C++ Language?
Swapping means exchanging data. In C++, swapping can be done by using two methods. First is swapping using third variable i.e. temporary variable and second is without using the third variable. In this section, we are going to see how to swap two and three numbers using both methods.
Example #1
Swapping two numbers Using the third variable.
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num, second_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num <<endl;
temp_num = first_num; //first number is assigned to temp
first_num = second_num; //second number is assigned to first number
second_num = temp_num; //first number is assigned to secind number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num;
return 0;
}
Output:
Example #2
Swapping two numbers without using the third variable.
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num, second_num;
cout << "Enter first number: ";
cin >> first_num; //9
cout << "Enter second number: ";
cin >> second_num; //10
cout << "Before swapping " << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
first_num = first_num * second_num; //9 * 10 = 90
second_num = first_num / second_num; // 90 / 10 = 9
first_num = first_num / second_num; // 90 / 9= 10
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl; 10
cout << "Second number: " << second_num << endl; //9
return 0;
}
Output:
Example #3
Swapping three numbers in C++ Using the third variable.
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num, second_num, third_num, temp_num;
cout << "Enter first number: "; //allow user to add first number
cin >> first_num;
cout << "Enter second number: "; //allow user to add second number
cin >> second_num;
cout << "Enter third number: "; //allow user to add third number
cin >> third_num;
cout << "Before swapping" << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: "<< third_num << endl;
temp_num =first_num;
first_num = second_num; //second number is assigned to first number
second_num = third_num; //third number is assigned to second number
third_num = temp_num; //first number is assigned to third number
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: " << third_num << endl;
return 0;
}
Output:
Example #4
Swapping three numbers without using the third variable.
Code:
#include<iostream>
using namespace std;
int main()
{
int first_num, second_num, third_num;
cout << "Enter first number: ";
cin >> first_num; //10
cout << "Enter second number: ";
cin >> second_num; //5
cout << "Enter third number: ";
cin >> third_num; //20
cout << "Before swapping" << endl;
cout << "First number: "<< first_num << endl;
cout << "Second number: " << second_num << endl;
cout << "Third number: " << third_num << endl;
first_num = first_num + second_num + third_num; // 10 + 5 + 20= 35
second_num = first_num - (second_num + third_num); // 35 - (5 + 20) = 10
third_num = first_num - (second_num + third_num); // 35 - (10 + 20) = 5
first_num = first_num - (second_num + third_num); 35 - (10 + 5) = 20
cout << "After swapping" << endl;
cout << "First number: " << first_num << endl; //20
cout << "Second number: "<< second_num << endl; //10
cout << "Third number: " << third_num << endl; //5
return 0;
}
Output:
Conclusion
In this article, we have seen how to swap two and three numbers in C++ using the third variable and without using the third variable. I hope you’ll find this article helpful.
Recommended Articles
This is a guide to Swapping in C++. Here we have discussed the working of Swapping in the C++ Language along with Examples and Outputs. You may also look at the following article to learn more –