Swap() c++ – C++ Program to Swap Two Numbers

Swap() c++: In the previous article, we have discussed about C++ : Map Tutorial Part 3: Using User defined class objects as keys in std::map. Let us learn how to Swap Two Numbers in C++ Program.

Different Methods to Swap Two Variables in C++

In this article, we can understand and write different methods in c++ to swap 2 variables. Let see all the methods one by one.

Method 1-By using a temporary variable

In this method, we will see how we can swap 2 variables in c++ with the help of a temporary variable. First, we see the program and then we see how this program work.

#include <iostream>
using namespace std;

int main() {
    int a,b,temp=0;
    a=10;
    b=5;
    
    cout<<"Value of a before swapping -> "<<a<<endl;
    cout<<"value of b before swapping -> "<<b<<endl;
    
    // logic to swap 2 variables
    temp=a;
    a=b;
    b=temp;
    
    cout<<"Value of a after swapping -> "<<a<<endl;
    cout<<"value of b after swapping -> "<<b<<endl;
    
    return 0;
}

Output

Value of a before swapping -> 10
value of b before swapping -> 5
Value of a after swapping -> 5
value of b after swapping -> 10

So here we see that value of a and b swapped. Now we will understand how this program run.

Explanation

In the program first, we take 3 variables a,b and temp and initialize them with values 10,5 and 0 respectively. After that, we write 3 lines of code which is the main reason for the swapping of variables.

  1. temp=a: Previously temp has 0 as the value stored in it while a has 10 as the value stored in it. But when this line of code runs the value of a is assigned to temp without any change in the value of a. Now temp has value 10 and a also has value 10.
  2. a=b: Now a has value 10 stored in it while b has value 5 stored in it. But when this line of code runs than the value of b is assigned to a without any change in the value of b. Now a has value 5 and b has value 5.
  3. b=temp: Now b has value 5 stored in it while temp has value 10 stored in it. But when this line of code runs then the value of temp is assigned to b without any change in the value of temp. Now temp has value 10 and b has value 10.

Hence we see this after running this code value of a and b swaps.

Method 2-Without using the temporary variable

In the previous method, we see that we have to use a temporary variable to swap 2 variables. But now we will see how we can do this task without taking the help of any temporary variable. First we write the code for this and then understand how this program run.

#include <iostream>
using namespace std;

int main() {
    int a,b;
    a=10;
    b=5;
    
    cout<<"Value of a before swapping -> "<<a<<endl;
    cout<<"value of b before swapping -> "<<b<<endl;
    
    // logic to swap 2 variables
    a=a+b;
    b=a-b;
    a=a-b;
    
    cout<<"Value of a after swapping -> "<<a<<endl;
    cout<<"value of b after swapping -> "<<b<<endl;
    
    return 0;
}

Output

Value of a before swapping -> 10
value of b before swapping -> 5
Value of a after swapping -> 5
value of b after swapping -> 10

Here we see that we swap values of a and b without using any temporary variable.Let understand how this program runs.

Explanation

The same as the previous method here also the main work is of 3 lines of code written after logic to swap 2 variables comment. Let understand these lines one by one.

  1. a=a+b: Previously a has 10 as the value stored in it. But when this line of code runs the value of a+b is assigned to a. Now a also has a value of 15.
  2. b=a-b: Now a has value 15 stored in it while b has value 5 stored in it. But when this line of code runs then the value of a-b is assigned to b. Now a has value of 15 and b has a value of 10.
  3. a=a-b: Now b has value 10 stored in it while a has value 5 stored in it. But when this line of code runs than the value of a-b is assigned to a. Now a has a value of 5 and b has a value of 10.

So here we understand how both the variables swapped.

Method 3: By using call by address

To understand this method one should have a basic knowledge of pointers. Because this method is a combination of method 1 and pointers. First, we write a code for this and then understand how this method work.

#include <iostream>
using namespace std;

void swap(int *a, int *b)
{
    int temp;
    temp=*a;
    *a=*b;
    *b=temp;
}

int main() {
    int a,b;
    a=10;
    b=5;
    
    cout<<"Value of a before swapping -> "<<a<<endl;
    cout<<"value of b before swapping -> "<<b<<endl;
    
    swap(&a,&b);
    cout<<"Value of a after swapping -> "<<a<<endl;
    cout<<"value of b after swapping -> "<<b<<endl;
    
    return 0;
}

Output

Value of a before swapping -> 10
value of b before swapping -> 5
Value of a after swapping -> 5
value of b after swapping -> 10

Here we see how we change the values with the help of pointers. Let understand the code.

Explanation

The first thing we have to notice that we pass the address of a and b in the function so any changes are done in their address value the same changes will also be reflected in the variable also. And the changes we done are the same as the changes we did in method 1.

Method 4: Call by reference

To understand this method one should have a knowledge of what a reference variable is. Let us write a code for this.

#include <iostream>
using namespace std;

void swap(int &a, int &b)
{
    int temp;
    temp=a;
    a=b;
    b=temp;
}

int main() {
    int a,b;
    a=10;
    b=5;
    
    cout<<"Value of a before swapping -> "<<a<<endl;
    cout<<"value of b before swapping -> "<<b<<endl;
    
    swap(a,b);
    cout<<"Value of a after swapping -> "<<a<<endl;
    cout<<"value of b after swapping -> "<<b<<endl;
    
    return 0;
}

Output

Value of a before swapping -> 10
value of b before swapping -> 5
Value of a after swapping -> 5
value of b after swapping -> 10

Method 5: By using the inbuilt swap() function of c++

In c++ we have an inbuilt swap function by which we can swap the value of variables. We can directly use this inbuilt function and our task will be done.

#include <bits/stdc++.h>
using namespace std;

int main() {
    int a,b;
    a=10;
    b=5;
    
    cout<<"Value of a before swapping -> "<<a<<endl;
    cout<<"value of b before swapping -> "<<b<<endl;
    
    swap(a,b);
    cout<<"Value of a after swapping -> "<<a<<endl;
    cout<<"value of b after swapping -> "<<b<<endl;
    
    return 0;
}

Output

Value of a before swapping -> 10
value of b before swapping -> 5
Value of a after swapping -> 5
value of b after swapping -> 10

So these are the methods to swap variables in c++.

Sample C++ code is useful for the beginners and experts to understand and start practising them.