C++ Program to Swap Two Numbers
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 = 2Input : a = 0, b = 1
Output : a = 1, b = 0
There are 8 ways to swap two numbers in C++
- Using a third variable.
- Without using a third variable.
- Using Call by Reference.
- Using swap() function.
- Using Bitwise Operator.
- Using Friend Function.
- Using arithmetic operators : * and /
- Short Way
Let’s start discussing each of these methods in detail.
1. Using 3rd Variable
The idea is simple in this approach:
- Assign a to a temp variable: temp = a
- Assign b to a: a = b
- 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 codeint 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;} |
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:
- Assign to b the sum of a and b i.e. b = a + b.
- Assign to a difference of b and a i.e. a = b – a.
- 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 codeint 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;} |
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:
- Create a swap function that will swap two numbers using the third variable temp as discussed in method 1(using 3rd variable).
- 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 Referencevoid Swap_two_numbers(int *x, int *y){ int temp; temp = *x; *x = *y; *y = temp;}// Driver codeint 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;} |
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 codeint 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;} |
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 codeint 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;} |
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 bint 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 Codeint main(){// Declaring and Initializing the Swap objectSwap s(4, 6);swap(s);return 0;} |
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 codeint 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;} |
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 codeint 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;} |
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.


Please Login to comment...