C++ Program to Swap Two Numbers
In C++, you can swap two numbers using a temporary variable like this:
int temp = a; a = b; b = temp; which exchanges the values of a and b.Examples
Inputa = 5, b = 10
OutputAfter swap: a = 10, b = 5
Inputa = -3, b = 7
OutputAfter swap: a = 7, b = -3
Inputa = 0, b = 0
OutputAfter swap: a = 0, b = 0
How to Think About It
To swap two numbers, think of holding one number temporarily so you don't lose it when you overwrite it. You store the first number in a temporary place, then replace the first number with the second, and finally put the stored number into the second variable.
Algorithm
1
Get the two numbers to swap.2
Store the first number in a temporary variable.3
Assign the second number to the first variable.4
Assign the temporary variable's value to the second variable.5
Print the swapped values.Code
cpp
#include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; int temp = a; a = b; b = temp; cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
Output
Before swap: a = 5, b = 10
After swap: a = 10, b = 5
Dry Run
Let's trace swapping a = 5 and b = 10 through the code
1
Initial values
a = 5, b = 10
2
Store a in temp
temp = 5
3
Assign b to a
a = 10
4
Assign temp to b
b = 5
| Step | a | b | temp |
|---|---|---|---|
| Initial | 5 | 10 | - |
| Store a | 5 | 10 | 5 |
| Assign b to a | 10 | 10 | 5 |
| Assign temp to b | 10 | 5 | 5 |
Why This Works
Step 1: Temporary storage
We use temp to hold the value of a so it is not lost when a is overwritten.
Step 2: Overwrite first variable
We assign the value of b to a, so now a has the second number.
Step 3: Restore second variable
We assign the stored value in temp to b, completing the swap.
Alternative Approaches
Using arithmetic operations
cpp
#include <iostream> using namespace std; int main() { int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; a = a + b; b = a - b; a = a - b; cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
This method swaps without extra memory but can cause overflow if numbers are large.
Using std::swap function
cpp
#include <iostream> #include <algorithm> using namespace std; int main() { int a = 5, b = 10; cout << "Before swap: a = " << a << ", b = " << b << endl; swap(a, b); cout << "After swap: a = " << a << ", b = " << b << endl; return 0; }
This is the simplest and safest way using the standard library.
Complexity: O(1) time, O(1) space
Time Complexity
Swapping two numbers takes constant time because it involves only a fixed number of operations.
Space Complexity
Only a small fixed amount of extra space is used for the temporary variable, so space complexity is constant.
Which Approach is Fastest?
All methods run in constant time; using std::swap is recommended for readability and safety.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Temporary variable | O(1) | O(1) | Simple and clear swaps |
| Arithmetic operations | O(1) | O(1) | No extra memory but risk of overflow |
| std::swap function | O(1) | O(1) | Clean, safe, and recommended |
Use
std::swap(a, b); for a clean and safe swap in C++.Forgetting to use a temporary variable or swapping incorrectly, which causes both variables to have the same value.