Swap numbers in C++
Swap numbers
Swapping refers to interchanging values between two variables. Swapping is important and easy to understand programming logic in the world of coding. Though it is used in the programming only, in special cases, it can be another practice to solve problems that need some logical order.
For example: If we take two variables say A and B and assign them values 10 and 20 respectively. The values after swapping to, A=20 and B=10.
Let's discuss the logic behind it.
There are two methods of swapping numbers: with or without a third variable. Let the third variable be temp that can also be called 'temporary variable.'
A temporary variable is used to hold the values during interchanging values. This value is later passed onto other variables after the value is swapped.
Swapping using a temporary or third variable:
As discussed earlier, we can swap numbers in C++ with or without using a third variable. Let us first swap numbers by using a temporary variable.
#include<iostream> using namespace std; int main() { int a=10,b=20; //initializing a & b with values int c; cout<<"Before Swapping: "<<a<<" "<<b<<" "<<endl; c=a; //swapping operation a=b; b=c; cout<<"After Swapping: "<<a<<" "<<b<<" "<<endl; return 0; }
Output:
Before Swapping: 10 20 After Swapping: 20 10
Explanation:
In the above code, we can clearly see that a temporary or a third variable c has been used to hold the values while swapping. Initially, a & b have been assigned values 10 and 20 respectively.
The next operation is to pass the value of a to variable c and then assign the value of b to a. Later the value of a which had 10 earlier now has 20 as value and b now has been assigned 10 by the third variable c.
Swapping without using a third variable:
Let us now discover how swapping can be performed without using a third variable.
The swapping operation can be performed without using third variables in two ways:
- Using * and /
#include<iostream> using namespace std; int main() { int a=10,b=20; cout<<"Before swapping: "<<a<<" "<<b<<" "<<endl; a=a*b; //a=200(10*20) b=a/b; //a= 10(200/20) a=a/b; //a=20(200/10) cout<<"After swapping: "<<a<<" "<<b<<" "<<endl; return 0; }
Output:
Before swapping: 10 20 After Swapping: 20 10
Explanation:
Here, we have used multiplication ( * ) and division( / ) operator to swap the values of a & b. The logic is quite simple to understand from the code itself.
Let us now look at the second way of swapping.
- Using + and -
int main() { int a=10,b=20; cout<<"Before swapping: "<<a<<" "<<b<<" "<<endl; a=a+b; //a=30(10+20) b=a-b; //a=10 (30-20) a=a-b; //a=20(30-10) cout<<"After swapping: "<<a<<" "<<b<<" "<<endl; return 0; { int a=10,b=20; cout<<"Before swapping: "<<a<<" "<<b<<" "<<endl; a=a+b; //a=30(10+20) b=a-b; //a=10 (30-20) a=a-b; //a=20(30-10) cout<<"After swapping: "<<a<<" "<<b<<" "<<endl; return 0; }
Output:
Before swapping: 10 20 After Swapping: 20 10
Explanation:
Here, we have used addition( + ) and subtraction( - ) operators to swap the values of a & b. The logic is quite simple to understand from the code itself.
More about swapping:
Swapping in numbers in any programming language is an easy concept of exchanging or interchanging values. We can also do this by taking input from the user and then interchange the values using simple operations. There are no hard and fast rules to swap numbers in C++. Earlier we used two methods where first method uses ( * ) &( / ) operators and second method uses ( + ) * ( - ) operators. It could have been also done in different ways, which provides a method for every beginner or an expert to swap numbers according to their level of understanding. The above examples are the simplest ways for a beginner in programming to understand how the swapping operation works in C++.
To be more practical, one needs to practice these swapping techniques in order to solve other problems where other logic can be easily implemented. But in general terms, it is recommended to use these techniques. It is easier to understand how the working of each line takes place. C++ makes it easier to understand because of its simple syntax.