In this tutorial, we are going to write a C++ program to swap two numbers. This is the most asked question of any programming languages, So in this article, we will see different-different ways to swap two numbers using C++.
If you are beginner in C++, Then this tutorial will very helpful for you.
In previous tutorial, we have seen how to add two numbers using C++.
In this guide we will see different techniques to write a c++ program to swap two numbers, Here we will swap two numbers using the third variable as well as without using the third variable
Headings of Contents
C++ Program To Swap Two Numbers
C++ provides various options to write C++ Program To Swap Two Numbers. Here we will write a different C++ program to swap two numbers. This swapping program is mostly asked by the interviewer in C++ coding round interviews.
Example 1:
In this example we will write C++ program to swap two numbers.
// Online C++ compiler to run C++ program online
#include <iostream>
using namespace std;
int main()
{
int first, second, temp;
// get first number from user
cout<<"Enter First Number:- ";
cin>>first;
//get second number from user
cout<<"Enter Second Number:- ";
cin>>second;
//Display both number before swapping
cout<<"\n\nBefore swapping";
cout<<"\nFirst Number is:- "<<first;
cout<<"\nSecond Number is:- "<<second;
// Swapping logic
temp = first;
first = second;
second = temp;
//Display both number after swapping
cout<<"\n\nAfter swapping";
cout<<"\nFirst Number is:- "<<first;
cout<<"\nSecond Number is:- "<<second;
return 0;
}
Output
Enter First Number:- 56
Enter Second Number:- 78
Before swapping
First Number is:- 56
Second Number is:- 78
After swapping
First Number is:- 78
Second Number is:- 56
Explain Code:
We have explained our code according to the above example.
- First, we create three variables first, second, and temp.
- Asked two values from the user and store them inside the first and second variables.
- Print both first and second variables value on the screen.
- Then, we assigned the first variable value inside the temp variable using temp = first code. temp value becomes 56.
- Then, we store the second variable value inside the first variable using the first = second code. first become 78.
- Again, we stored the temp variable value inside the second variable using second = temp code. second become 56.
- Print both variables first and second value after swapping.
Example 2:
In this example, we will write a C++ program to swap two numbers without third variable.
#include <iostream>
using namespace std;
int main()
{
int first, second;
// get first number from user
cout<<"Enter First Number:- ";
cin>>first;
//get second number from user
cout<<"Enter Second Number:- ";
cin>>second;
//Display both number before swapping
cout<<"\n\nBefore swapping";
cout<<"\nFirst Number is:- "<<first;
cout<<"\nSecond Number is:- "<<second;
//Display both number after swapping
// Swapping logic
first = first * second;
second = first/second;
first = first/second;
cout<<"\n\nAfter swapping";
cout<<"\nFirst Number is:- "<<first;
cout<<"\nSecond Number is:- "<<second;
return 0;
}
Output
Enter First Number:- 10
Enter Second Number:- 15
Before swapping
First Number is:- 10
Second Number is:- 15
After swapping
First Number is:- 15
Second Number is:- 10
Explain Code
- First, we create three variables first, second, and temp.
- Asked two values from the user and store them inside the first and second variables.
- Print both first and second variables value on the screen.
- We multiply both first and second variable value and assigned into the first variable using first = first * second; code.
- Now, we divide first by second variable and store quotient into second using second = first/second; code.
- Then, Again we divide first by second and store quotient into the first variable using first = first/second; code.
- Print both variables first and second value after swapping.
Example 3:
Here we will write C++ program to swap two numbers without third variable.
#include <iostream>
using namespace std;
int main()
{
int first, second;
// get first number from user
cout<<"Enter First Number:- ";
cin>>first;
//get second number from user
cout<<"Enter Second Number:- ";
cin>>second;
//Display both number before swapping
cout<<"\n\nBefore swapping";
cout<<"\nFirst Number is:- "<<first;
cout<<"\nSecond Number is:- "<<second;
//Display both number after swapping
// Swapping logic
first = first + second;
second = first - second;
first = first - second;
cout<<"\n\nAfter swapping";
cout<<"\nFirst Number is:- "<<first;
cout<<"\nSecond Number is:- "<<second;
return 0;
}
Output
Enter First Number:- 12
Enter Second Number:- 22
Before swapping
First Number is:- 12
Second Number is:- 22
After swapping
First Number is:- 22
Second Number is:- 12
Conclusion:
In this tutorial, you have learned how to write C++ program to swap two numbers. If you are a beginner in C++ programming, Then this C++ program to swap two numbers example most important for you because this question mostly asked by any interviewer.
I hope this tutorial will help you. If you like this article, please comment and share it with your friends who want to learn C++ programming and write this type of C++ example.
For More Information:- Click Here