C++ Programming
Swap two numbers by C++
Swap two numbers : If there are two variable named x and y. And the value of x and y are following;
x = 10;
y = 20;
Then the swap number program will makes it like as bellow;
x = 20;
y = 10;
Swap number means interchanging the value of two variables. We can make the program to swap two number using a third variable or without it. Here we will see some C++ program to swap two numbers.
In our C programming guide we have discussed about swap two numbers in C and here we will do same thing with C++ only. So, let’s get started with C++ code.
Swap two numbers using a third variable in C++
- At first we will take the value of x and y from the user.
- Then we will store the value of x into z.
- Then we will assign the value of y into x
- After that we will assign the value of z to y.
// swap number program in c++
#include <iostream>
using namespace std;
int main(){
int x, y, z; // variable declaration
cout << "Enter value of x here : ";
cin >> x;
cout << "Enter value of y here : ";
cin >> y;
z = x; // swapping numbers
x = y;
y = z;
cout << "\nValue of x = " << x << endl;
cout << "Value of y = " << y << endl;
return 0;
}
Output of this swap numbers program

Swap two numbers without third variable
Here we will implement some different logic to swap two numbers without using a third variable. We can do it in different way. Let’s see some logic to swap two numbers without third variable.
First method:
In this bellow program we will use * and / operator to swap two numbers.
Consider, x = 5 and y = 7.
Then (x * y) = (5 * 7) = 35
And (xy / y) = x where (xy / x) = y
That means, (35 / 7) = 5 and (35 / 5) = 7.
// swap two number using * and / operators
#include <iostream>
using namespace std;
int main(){
int first = 15, second = 25;
cout<<"Before swapping value of : \n" << "first variable = " << first << endl;
cout << "second variable = " << second << endl;
first = first * second;
second = first / second;
first = first / second;
cout<<"\nAfter swapping value of : \n" << "first variable = " << first << endl;
cout << "second variable = " << second << endl;
return 0;
}
Output of program:
Before swapping value of :
first variable = 15
second variable = 25
After swapping value of :
first variable = 25
second variable = 15
Second method:
Now we will swap two number using + and – operators.
Consider x = 4 and y = 6;
Now, (x + y) = (4 + 6) = 10;
We will assign 10 to the variable x, that means x = 10 now.
Now, y = (x – y) = (10 – 6) = 4
And x = (x – y) = (10 – 4) = 6; (Because y = 4 now)
// swap two number using + and - operators
#include <iostream>
using namespace std;
int main(){
int first = 10, second = 20;
cout << "Before swapping : \nfirst variable = " << first << endl;
cout << "second variable = " << second << endl;
first = first + second;
second = first - second;
first = first - second;
cout << "\nAfter swapping : \nfirst variable = " << first << endl;
cout << "second variable = " << second << endl;
return 0;
}
Output of this swap two numbers program:
Before swapping :
first variable = 10
second variable = 20
After swapping :
first variable = 20
second variable = 10
Previous page: Reverse number
Next page: Fibonacci series