To swap two numbers means exchange their values: after the swap, the first variable holds what the second had, and the second holds what the first had.
Remember
Before: a = 5, b = 10
After: a = 10, b = 5
Default interview answer:
temp = a
a = b
b = temp
Method
Idea
Notes
Temp variable
Save, overwrite, restore
Clearest and safest
Arithmetic
a+b, then subtract
Can overflow int
XOR
Three ^= steps
Integers only; interview trick
Approach
How to Swap Two Numbers in C++
Four clear steps for the temp method — then optional no-temp variants.
Store the first value in temp.
Copy the second value into the first variable.
Copy temp into the second variable.
Or, if asked, show arithmetic / XOR as alternatives (mention overflow and readability).
Method
Idea
Time
Best for
Temp variable
temp = a; a = b; b = temp;
O(1)
Interviews, real code
Arithmetic
Encode both in a sum, then peel
O(1)
No-temp follow-up
XOR
a ^= b three times
O(1)
Integer bit trick
Pseudocode
Pseudocode
function SwapTemp(a, b):
temp = a
a = b
b = temp
return a, b
function SwapArithmetic(a, b):
a = a + b
b = a - b
a = a - b
return a, b
function SwapXor(a, b):
if a != b:
a = a XOR b
b = b XOR a
a = a XOR b
return a, b
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Default
Use a temporary variable
Temp steps
temp = a; a = b; b = temp;
Equal values
Swap still works; result unchanged
Arithmetic risk
a + b may overflow int
Modern C++
std::swap(a, b); is fine in real code
Classic check
5, 10 → 10, 5
Try it
Live Preview
Enter two integers and swap them with the temporary-variable method.
Whole numbers only. Tap a chip or type a pair.
Result
Before: a = 5, b = 10
temp = 5
a = 10
b = 5
After: a = 10, b = 5
Trace
Worked Walkthrough — a = 5, b = 10
Trace the temporary-variable swap step by step.
Calculation
Start: a = 5, b = 10
Step 1: temp = a → temp = 5
Step 2: a = b → a = 10
Step 3: b = temp → b = 5
Result: a = 10, b = 5
Code
C++ Programs
Start with a temporary variable — the interview default. Then show arithmetic and XOR as no-temp alternatives.
Example 1 — Swap Using Temp Variable
Save the first value, overwrite it, then restore into the second variable.
Near INT_MAX, a + b can overflow — prefer temp in real code.
Good follow-up when asked to swap without a temporary variable.
Example 3 — Swap Without Temp (XOR Trick)
Three XOR steps trade bit patterns. Keep it as an interview curiosity.
C++
#include <iostream>
using namespace std;
int main() {
int a = 5;
int b = 10;
if (a != b) {
a ^= b;
b ^= a;
a ^= b;
}
cout << "After swapping: a = " << a
<< ", b = " << b << endl;
return 0;
}
Output
After swapping: a = 10, b = 5
How It Works
a ^= b mixes both bit patterns into a.
b ^= a recovers the original a into b.
a ^= b recovers the original b into a.
The a != b guard is a safe habit when mentioning aliasing edge cases.
Prefer temp for readability; mention XOR only if asked.
Edge Cases & Pitfalls
Check these before calling the solution done.
Equal
Same values
Swap still runs; both variables keep the same number.
Overflow
Arithmetic risk
a + b can overflow int. Prefer a temp variable for large values.
XOR
Integers only
Bitwise XOR is not for floats or general objects. Temp works more broadly.
Readability
Temp first
In interviews and production, lead with temp (or std::swap). Treat XOR as optional.
Analysis
Time and Space Complexity
Program
Time
Extra space
Temp variable
O(1)
O(1)
Arithmetic
O(1)
O(1)
XOR
O(1)
O(1)
A swap touches a fixed number of variables — constant time and space.
Remember
Key Takeaways
Goal: exchange the values stored in two variables.
Default:temp = a; a = b; b = temp;
No-temp options: arithmetic or XOR — mention overflow and readability.
Test set: (5,10) → (10,5); (4,4) stays; negatives work with temp.
One line: save one value, overwrite it, then put the saved value into the other variable.
Frequently Asked Questions
After a swap, the first variable holds the old second value, and the second holds the old first value.
Not always. Arithmetic or XOR can swap without a named temp, but a temporary variable is the clearest and safest default. In real code, std::swap is idiomatic.
Swap still works; both values remain the same.
Yes. Temp and arithmetic work for any ints. XOR also works for integers.
All common swap methods are O(1) time and O(1) extra space.