Swap Two Numbers in C++
Swap two integers in C++ with a temporary variable and print both values before and after. Shows why the temp is needed and what std::swap and XOR tricks do.
1#include <iostream>2using namespace std;34int main() {5 int a, b, temp;67 cout << "Enter first number (a): ";8 cin >> a;910 cout << "Enter second number (b): ";11 cin >> b;1213 cout << "Before swapping: a = " << a << ", b = " << b << endl;1415 // Swapping logic16 temp = a;17 a = b;18 b = temp;1920 cout << "After swapping: a = " << a << ", b = " << b << endl;2122 return 0;23}
Sample Output
Before swapping: a = 5, b = 10
The original values, printed first so you can compare.
After swapping: a = 10, b = 5
The values have traded places.
In this one
- Why swapping needs a third variable
- How three assignments trade two values
- What std::swap does for you
- Why the add-subtract and XOR tricks are risky
How it works
- 1
Read a and b from the user (5 and 10 in the sample).
- 2
Print the values before the swap.
- 3
temp = a saves 5.
- 4
a = b copies 10 into a.
- 5
b = temp puts the saved 5 into b, then the new values are printed.
Swap trace for a = 5, b = 10
Values after each line of the swap.
- 1.Start: a = 5, b = 10, temp = ?
- 2.temp = a → a = 5, b = 10, temp = 5
- 3.a = b → a = 10, b = 10, temp = 5
- 4.b = temp → a = 10, b = 5, temp = 5
Edge cases
Same number twice
Enter 7 and 7.
The swap still runs and both stay 7. Nothing breaks, which is one advantage of the temp version over the XOR trick when both names refer to the same variable.
Skipping temp
Write a = b; b = a; without temp.
After a = b, the old value of a is gone, so both end up holding the value of b. That is the bug the temp variable prevents.
What's going on
This C++ program swaps two integers using a temporary variable. After a correct swap, a and b have traded values.
int a, b, temp;
Three integers. a and b come from cin. temp is only a parking spot.
cin >> a; then cin >> b;
Sample: 5 and 10.
cout << "Before swapping: a = " << a << ", b = " << b << endl;
Print first. If you skip this, you can't tell whether the swap ran.
temp = a;
Park 5. Without this, the next assignment throws 5 away.
a = b;
a is 10. b is still 10. Stop here and both are 10.
b = temp;
b gets the parked 5. After: a = 10, b = 5. Matches the output.
std::swap(a, b) in <algorithm> does the same three assignments. XOR swap (a ^= b; b ^= a; a ^= b;) exists and breaks when a and b are the same location. Three lines with temp is the version to write on paper.
Swapping without temp using a = a + b; b = a - b; a = a - b; overflows for large ints. Don't.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
After temp = a; a = b; with a = 3, b = 8, what are a and b?
Pick an answer to check it.
Question 2
Why can the XOR swap fail?
Pick an answer to check it.
Questions people ask
Is there a built-in swap?
Yes. std::swap(a, b) in <utility> (also reachable via <algorithm>) swaps two values of the same type.
Why not use a = a + b; b = a - b; a = a - b;?
It works for small numbers but a + b can overflow an int, which is undefined behavior. The temp version never overflows.
Does swapping work for strings too?
Yes. The same three lines work with string variables, and std::swap is very fast for strings.