← Back

24. Swap Two Numbers

Easy

📝 Problem

Given two numbers, Swap those two numbers with temporary variable.

Input Format:
Accept two integer values as input
Output Format:
Print the output as follows:
num1=respective value
num2=respective value
Constraints:
1 ≤ N1, N2 ≤ 10^9
Sample Input 1:
2
36
Sample Output 1:
num1=36
num2=2
Sample Input 2:
10 20
Sample Output 2:
num1=20
num2=10
💡 Key Concepts

What You'll Learn:

  • Variable Assignment: Understanding how values are stored and transferred
  • Temporary Variables: Using a third variable to preserve data
  • Swap Algorithm: One of the most fundamental programming patterns
  • Escape Sequences: Using \n for newline in output

🔍 How Swapping Works

📌 Step-by-Step Visualization:
// Initial state: a=2, b=36
long long a = 2, b = 36, temp;

// Step 1: Save 'a' to temporary variable
temp = a;  // temp=2, a=2, b=36

// Step 2: Copy 'b' to 'a'
a = b;     // temp=2, a=36, b=36

// Step 3: Copy temp to 'b'
b = temp;  // temp=2, a=36, b=2

// Final state: a=36, b=2 ✓ Swapped!
💡 Why We Need a Temporary Variable
Without a temporary variable, you'd lose data! If you do a = b first, the original value of a is overwritten and lost forever. The temp variable acts like a safe storage to preserve the value during the swap.

🧩 The Classic Swap Pattern

📌 The Three-Step Pattern:
// Think of it like swapping drinks between two cups:

// Cup A has Coffee ☕, Cup B has Tea 🍵
// You need an empty Cup (temp) to swap them!

temp = a;  // Pour Coffee into empty cup
a = b;     // Pour Tea into Cup A
b = temp;  // Pour Coffee into Cup B

// Result: Cup A has Tea 🍵, Cup B has Coffee ☕

🎯 Common Mistakes to Avoid

📌 ❌ Wrong Way (Data Loss!):
long long a = 2, b = 36;

a = b;  // a=36, b=36 (original 'a' lost!)
b = a;  // a=36, b=36 (both have same value!)

// ❌ Failed! Both variables now equal 36
⚠️ Remember: The order matters! Always save the first variable to temp BEFORE overwriting it.

🚀 Alternative Swapping Methods

While we use a temp variable here, there are other methods (for reference):

📌 Method 1: Using temp (This Question) ✅
temp = a;
a = b;
b = temp;
📌 Method 2: Arithmetic (No temp needed)
a = a + b;  // a=38, b=36
b = a - b;  // a=38, b=2
a = a - b;  // a=36, b=2
// Risk: Can overflow with large numbers!
📌 Method 3: XOR Bitwise (Advanced)
a = a ^ b;
b = a ^ b;
a = a ^ b;
// Clever but less readable!
📌 Method 4: C++ swap() Function
#include <algorithm>
swap(a, b);  // Built-in function!
💡 Why Use temp Method?
The temporary variable method is:
  • Clear and readable - Easy to understand
  • Safe - No overflow risks
  • Works with any type - Not just integers
  • Industry standard - Used in production code

📚 Real-World Applications

Swapping is fundamental in:

  • Sorting Algorithms - Bubble sort, selection sort use swapping
  • Array Manipulation - Reversing arrays, shuffling elements
  • Data Structure Operations - Linked list node swapping
  • Graphics Programming - Double buffering technique
  • Game Development - Player position swaps, turn switching

Mastering this simple pattern is essential - you'll use it countless times in your programming journey!

💻 Solution Code
#include <iostream>

using namespace std;

int main() {
    long long a, b, temp;
    
    cin >> a >> b;
    
    temp = a;
    a = b;
    b = temp;
    
    cout << "num1=" << a << "\nnum2=" << b;
    return 0;
}