In this tutorial, you will learn how to write C++ Programs to swap two numbers stored in variables. The numbers could be integers, float, double, long, etc. We shall cover different techniques to swap numeric values in two variables.
C++ Swap Two Numbers
In our first example, we shall write a C++ program that uses a temporary variable to swap two numbers.
In our second example, we shall write a Java program that swaps two numbers in place. No third variable is used. We shall use the existing two variables holding the two numbers.
1. Swap two numbers using temporary Variable
In this example, we take two numbers in two variables. Then we shall use a temporary variable to hold one of the numbers while we are trying to swap the values between the two variables.
Algorithm
Following is the algorithm we shall use to swap the given two numbers using a third variable.
- Start.
- Read a number in num1.
- Read a number in num2.
- Declare a variable temp.
- Assign temp with num1.
- Assign num1 with num2.
- Assign num2 with temp.
- Print num1 and num2.
- Stop.
C++ Program
#include <iostream>
using namespace std;
int main() {
int num1 = 12;
int num2 = 87;
int temp;
temp = num1;
num1 = num2;
num2 = temp;
cout << "num1 : " << num1 << endl;
cout << "num2 : " << num2 << endl;
}
Explanation
int num1 = 12;
declares num1
as integer and initializes num1
with 12
.
int num2 = 87;
declares num2
as integer and initializes num2
with 87
.
int temp;
declares temp as integer. We shall use this as temporary variable to hold one of the values of the two numbers.
temp = num1;
assigns value in num1
to temp
variable. So, we are saving the value of of num1
to temp
. Now we can do anything to num1
, because we have its value stored in other variable temp
. After this step, the variables have the values as shown in the following.
num1 = 12;
num2 = 87;
temp = 12;
num1 = num2;
assigns the value of num2
to num1
. So, num1
will have a value of 87
stored in it.
num1 = 87
num2 = 87
temp = 12
num2 = temp;
assigns the value of temp
to num2
. So, num2
will have a value of 12
stored in it.
num1 = 87
num2 = 12
temp = 12
We started with num1
and num2
having values of 12
and 87
respectively. After swapping, we are left with num1
and num2
having values 87 and 12 respectively.
We swapped the values in the variables.
Run the above C++ program, and you shall get the following output.
Output
num1 : 87
num2 : 12
2. Swap two numbers in place (without any temporary variable)
In this example, we take two numbers in two variables. We shall not use another temporary variable, but just these two variables to swap the numbers. Let us see how.
Algorithm
Following is the algorithm we shall use to swap the given two numbers using a third variable.
- Start.
- Read a number in num1.
- Read a number in num2.
- Assign num1 with num2+num1.
- Assign num2 with num1-num2.
- Assign num1 with num1-num2.
- Print num1 and num2.
- Stop.
C++ Program
#include <iostream>
using namespace std;
int main() {
int num1 = 12;
int num2 = 87;
num1 = num2+num1;
num2 = num1-num2;
num1 = num1-num2;
cout << "num1 : " << num1 << endl;
cout << "num2 : " << num2 << endl;
}
Explanation
int num1 = 12;
declares num1
as integer and initializes num1
with 12
.
int num2 = 87;
declares num2
as integer and initializes num2
with 87
.
num1 = num2+num1;
computes the sum of both num1
and num2
, and stores the result in num1
. After the execution of this statement, values of num1 and num2 would be as shown below.
num1 = 99
num2 = 12
num2 = num1-num2;
computes the difference of num1
and num2
, and stores the result in num2
. After the execution of this statement, values of num1
and num2
would be as shown below.
num1 = 99
num2 = 87
Please note that num2
contains the value of initial num1
.
num1 = num1-num2;
computes the difference of num1
and num2
, and stores the result in num1
. After the execution of this statement, values of num1
and num2
would be as shown below.
num1 = 12
num2 = 87
The values in the two variables are swapped.
Runt the above C++ program. You shall get the output in console, as shown below.
Output
num1 : 87
num2 : 12
Conclusion
In this C++ Tutorial, we learned how to swap two integers, in different ways. The process is same for float, int, or double. Just the datatype of num1
and num2
changes.