The Wayback Machine - https://web.archive.org/web/20230324230922/https://www.geeksforgeeks.org/cpp-program-to-swap-two-numbers/
Skip to content
Related Articles
Open in App
Not now

Related Articles

C++ Program to Swap Two Numbers

Improve Article
Save Article
  • Last Updated : 26 Feb, 2023
Improve Article
Save Article

Here, we will see how to swap two numbers using a C++ program. Below are the examples:

Input : a = 2, b = 3 
Output :  a = 3, b = 2

Input : a = 0, b = 1 
Output : a = 1, b = 0

There are 8 ways to swap two numbers in C++

  1. Using a third variable.
  2. Without using a third variable.
  3. Using Call by Reference.
  4. Using swap() function.
  5. Using Bitwise Operator.
  6. Using Friend Function.
  7. Using arithmetic operators : * and /
  8. Short Way 

Let’s start discussing each of these methods in detail.

1. Using 3rd Variable

The idea is simple in this approach:

  1. Assign a to a temp variable: temp = a
  2. Assign b to a: a = b
  3. Assign temp to b: b = temp

Below is the C++ program to implement the above approach:

C++




// C++ program to swap two
// numbers using 3rd variable
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a = 2, b = 3;
    
    cout << "Before swapping a = " <<
             a << " , b = " << b << endl;
    int temp;   
    temp = a;
    a = b;
    b = temp;
    cout << "After swapping a = " << a <<
            " , b = " << b << endl;
    return 0;
}

Output

Before swapping a = 2 , b = 3
After swapping a = 3 , b = 2

Time Complexity: O(1) as only constant operations are done.

Space Complexity: O(1) as no extra space has been used.

2. Without Using 3rd Variable (using + and -)

The following approach will be used here:

  1. Assign to b the sum of a and b i.e. b = a + b.
  2. Assign to a difference of b and a i.e. a = b – a.
  3. Assign to b the difference of b and a i.e. b = b – a.

Below is the C++ program to implement the above approach:

C++




// C++ program to swap two
// numbers without using 3rd
// variable
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a = 2, b = 3;
    
    cout << "Before swapping a = " <<
             a << " , b = " << b << endl;
 
    b = a + b;  
    a = b - a; 
    b = b - a; 
   
    cout << "After swapping a = " << a <<
            " , b = " << b << endl;
    return 0;
}

Output

Before swapping a = 2 , b = 3
After swapping a = 3 , b = 2

Time Complexity: O(1) as only constant operations are done.

Space Complexity: O(1) as no extra space has been used.

3. Using Call by Reference

The following approach will be used here:

  1. Create a swap function that will swap two numbers using the third variable temp as discussed in method 1(using 3rd variable).
  2. Call swap function with parameters a and b using call by reference approach.

Below is the C++ program to implement the above approach:

C++




// C++ program to swap two
// numbers using call by
// reference
#include<bits/stdc++.h>
using namespace std;
 
// Function which will swap
// two numbers using call by Reference
void Swap_two_numbers(int *x,
                      int *y)
{
    int temp;
    temp = *x;
    *x = *y;
    *y = temp;
}
 
// Driver code
int main()
{
    int a = 2, b = 3;
   
    cout << "Before swapping a = " <<
             a << " , b = " << b << endl;
     
      // Function call to swap
    Swap_two_numbers(&a;, &b;);
 
    cout << "After swapping a = " <<
             a << " , b = " << b << endl;
    return 0;
}

Output

Before swapping a = 2 , b = 3
After swapping a = 3 , b = 2

4. Using swap() Function

This approach uses the in-built swap() function to swap the two variables a and b. Below is the C++ program to implement the above approach:

C++




// C++ program to swap two
// numbers using swap()
// function
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a = 2, b = 3;
 
    cout << "Before swapping a = " <<
             a << " , b = " << b << endl;
     
      // Built-in swap function
    swap(a, b);
 
    cout << "After swapping a = " <<
             a << " , b = " << b << endl;
    return 0;
}

Output

Before swapping a = 2 , b = 3
After swapping a = 3 , b = 2

Time Complexity: O(1) as only constant operations are done.

Space Complexity: O(1) as no extra space has been used.

5. Using Bitwise Operator

The following approach will be used here:

Assign to “a” the XOR of “a” and “b” i.e. a = a ^ b.
Assign to “b” the XOR of “b” and “a” i.e. b = b ^ a. 
Assign to b the difference of “b” and “a” i.e. a = a ^ b.

Below is the C++ program to implement the above approach:

C++




// C++ program to swap two
// numbers using Bitwise Operator.
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
    int a = 7, b = 3;
   
    cout << "Before swapping a = " <<
             a << " , b = " << b << endl;
     
    a = a ^ b;
    b = b ^ a; // b = b ^ (a ^ b) = a
    a = a ^ b; // a = (a ^ b) ^ a = b
 
    cout << "After swapping a = " <<
             a << " , b = " << b << endl;
    return 0;
}

Output

Before swapping a = 7 , b = 3
After swapping a = 3 , b = 7

Time Complexity: O(1) as only constant operations are done.

Space Complexity: O(1) as no extra space has been used.

6. Using Friend function :

Approach:

Declare three variables i.e., a, b, and temp in a class Swap , and create a constructor for inputs. 
Declare a friend function in it. 
Define the friend function (outside the class scope). This can be done by taking arguments as call by reference to pass the copy of Swap Object. 
Perform the swap operation with swap variables, and swap values of a and b using temp variable.

Below is the C++ program to implement the above approach :

C++




// C++ Program to swap two numbers using friend function
#include <iostream>
using namespace std;
 
class Swap {
 
// Declare the variables : temp, a and b
int temp, a, b;
 
public:
Swap(int a, int b) //define a method Swap
{
this->a = a;
this->b = b;
}
 
friend void swap(Swap&); //calling friend function
};
 
void swap(Swap& s1) // method for swapping values
{
 
cout << "\nBefore Swapping: " << s1.a << " " << s1.b;
 
s1.temp = s1.a;
s1.a = s1.b;
s1.b = s1.temp;
cout << "\nAfter Swapping: " << s1.a << " " << s1.b;
}
 
// Driver Code
int main()
{
// Declaring and Initializing the Swap object
Swap s(4, 6);
swap(s);
return 0;
}

Output

Before Swapping: 4 6
After Swapping: 6 4

7. Using arithmetic operators : * and / 

Here, we will be using the arithmetic operators * and / to swap the values of two numbers.

Assign to b the multiplication of a and b i.e. b = a * b.
Assign to a division of b and a i.e. a = b / a.
Assign to b the division of b and a i.e. b = b / a.

Below is the C++ program to implement the above approach :

C++




// C++ program to swap two numbers using * and /
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
int a = 5, b = 8;
 
cout << "Values before swapping a = " <<
 a << " , b = " << b << endl;
 
b = a * b;
a = b / a;
b = b / a;
 
cout << "Values after swapping a = " << a <<
 " , b = " << b << endl;
return 0;
}

Output

Values before swapping a = 5 , b = 8
Values after swapping a = 8 , b = 5

Time Complexity: O(1) as only constant operations are done.

Space Complexity: O(1) as no extra space has been used.

8.   Improved and short way

This is a simplified approach in which we can swap the values of two numbers in one line, using basic operators.

Below is the C++ program to implement the above approach :

C++




// C++ program to swap two numbers using arithmetic operators.
#include<bits/stdc++.h>
using namespace std;
 
// Driver code
int main()
{
int a = 5, b = 8;
 
cout << "Values before swapping a = " <<
 a << " , b = " << b << endl;
 
b = a - b + (a = b);
 
cout << "Values after swapping a = " << a <<
 " , b = " << b << endl;
return 0;
}

Output

Values before swapping a = 5 , b = 8
Values after swapping a = 8 , b = 5

Time Complexity: O(1) as only constant operations are done.

Space Complexity: O(1) as no extra space has been used.


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!