C++ Program to Swap Two Numbers
Last Updated :
02 Jul, 2024
Swapping numbers is the process of interchanging their values. In this article, we will learn algorithms and code to swap two numbers in the C++ programming language.

1. Swap Numbers Using a Temporary Variable
We can swap the values of the given two numbers by using another variable to temporarily store the value as we swap the variables' data. The below algorithm shows how to use the temporary variable to swap values.
Algorithm
- Assign a to a temp variable: temp = a
- Assign b to a: a = b
- Assign temp to b: b = temp
C++ Program to Swap Two Numbers using a Temporary Variable.
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;
// temporary variable
int temp;
// appying swapping algorithm
temp = a;
a = b;
b = temp;
cout << "After swapping a = " << a << " , b = " << b
<< endl;
return 0;
}
OutputBefore swapping a = 2 , b = 3
After swapping a = 3 , b = 2
Complexity Analysis
- Time Complexity: O(1) as only constant operations are done.
- Space Complexity: O(1) as no extra space has been used.
2. Swap Numbers Without Using a Temporary Variable
We can also swap numbers without using the temporary variable. Unlike the previous method, we use some mathematical operations to swap the values.
Algorithm
- 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.
C++ Program to Swap Two Numbers Without Using a Temporary Variable.
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;
// applying algorithm
b = a + b;
a = b - a;
b = b - a;
cout << "After swapping a = " << a << " , b = " << b
<< endl;
return 0;
}
OutputBefore swapping a = 2 , b = 3
After swapping a = 3 , b = 2
Complexity Analysis
- Time Complexity: O(1), as only constant time operations are done.
- Space Complexity: O(1), as no extra space has been used.
3. Swap Two Numbers Using Inbuilt Function
C++ Standard Template Library (STL) provides an inbuilt swap() function to swap two numbers.
Syntax of swap()
swap(a, b);
where a and b are the two numbers.
C++ Program to Swap Two Numbers Using the Inbuilt swap() Function.
C++
// C++ program to swap two
// numbers using swap()
// function
#include <bits/stdc++.h>
using namespace std;
// Driver code
int main()
{
int a = 5, b = 10;
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;
}
OutputBefore swapping a = 5 , b = 10
After swapping a = 10 , b = 5
Complexity Analysis
- Time Complexity:Â O(1) as only constant operations are done.
- Space Complexity:Â O(1) as no extra space has been used.
Refer to the complete article How to swap two numbers without using a temporary variable? for more methods to swap two numbers.
Similar Reads
How to Swap Two Numbers Using Pointers in C++? Swapping the values of two numbers is a very common operation in programming, which is often used to understand the basics of variables, pointers, and function calls. In this article, we will learn how to swap two numbers using pointers in C++. Example Input:int x=10;int y=20;Output:int x=20;int y=1
3 min read
C++ Program to Swap characters in a String Given a String S of length N, two integers B and C, the task is to traverse characters starting from the beginning, swapping a character with the character after C places from it, i.e. swap characters at position i and (i + C)%N. Repeat this process B times, advancing one position at a time. Your ta
6 min read
C++ Program to Rotate digits of a given number by K Given two integers N and K, the task is to rotate the digits of N by K. If K is a positive integer, left rotate its digits. Otherwise, right rotate its digits. Examples: Input: N = 12345, K = 2Output: 34512 Explanation: Left rotating N(= 12345) by K(= 2) modifies N to 34512. Therefore, the required
2 min read
Swap Two Numbers Without Third Variable in C++ In C++, swapping two numbers means we need to exchange the value of two numbers. In this article, we will learn how to swap two numbers without using the third variable in C++. Example Input: a=10b=20Output:After swapping:a=20b=10Swap Two Numbers Without Using a Third VariableIn C++ we can swap two
2 min read
Swap Two Numbers using Function in C++ Swapping numbers means exchanging the values of the two numbers with each other. For Example, Before Swapping:a = 10, b = 22;After swapping:a = 22, b = 10In this article, we will write a program to swap two numbers using a function in C++. How to Swap Two Numbers Using Function in C++?We will pass t
3 min read