Swap Two Numbers

Beginner-friendly C++ program that shows how to swap two numbers using a temporary variable.

C++Beginner
C++
#include <iostream>
using namespace std;

int main() {
    int a, b, temp;
    
    cout << "Enter first number (a): ";
    cin >> a;
    
    cout << "Enter second number (b): ";
    cin >> b;
    
    cout << "Before swapping: a = " << a << ", b = " << b << endl;
    
    // Swapping logic
    temp = a;
    a = b;
    b = temp;
    
    cout << "After swapping: a = " << a << ", b = " << b << endl;
    
    return 0;
}

Output

Enter first number (a): 5
Enter second number (b): 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5

Swap Two Numbers in C++

This program teaches you how to swap the values of two variables in C++. Swapping means exchanging their values. If a starts with 5 and b starts with 10, after swapping a should become 10 and b should become 5. This example uses a very simple and common technique called the temporary variable method.

1. Header File

#include <iostream>

This header allows the program to use cout (for printing) and cin (for taking input).
It is required for basic input/output operations in C++.

2. Variable Declaration

The program declares three integer variables:

int a, b, temp;

Here is the purpose of each:

  • a → stores the first number
  • b → stores the second number
  • temp → used as a helper (temporary storage) during swapping

The variable temp acts like a third container to help us exchange the values safely.

3. Taking Input From the User

The program uses cin to take input:

cout << "Enter first number (a): ";
cin >> a;

cout << "Enter second number (b): ";
cin >> b;

The user types values, and those values are stored inside the variables a and b.
For example:
a = 5
b = 10

4. Showing Values Before Swapping

Before swapping happens, we print the original values:

cout << "Before swapping: a = " << a << ", b = " << b << endl;

This helps the user see what the values were before they get exchanged.

5. Swapping Logic (Step-by-Step)

The main part of the program is the swapping logic:

temp = a;
a = b;
b = temp;

Here is what happens:

Step 1: temp = a

  • The value of a (for example 5) is copied into temp.
  • Now temp = 5.

Step 2: a = b

  • The value of b (for example 10) is copied into a.
  • Now a = 10.

Step 3: b = temp

  • The value stored in temp (which is 5) is copied back into b.
  • Now b = 5.

After these steps, the values of a and b are completely swapped.

Why do we need temp?

Because if we directly write a = b; and then b = a;, the original value of a would be lost.
temp safely holds it.

6. Showing Values After Swapping

After swapping, we print the new values:

cout << "After swapping: a = " << a << ", b = " << b << endl;

This confirms that the values have been exchanged successfully.

7. return 0;

This ends the program and signals successful completion.

Summary

  • The program reads two numbers from the user.
  • It swaps their values using a temporary variable.
  • Swapping logic uses a simple three-step process.
  • This method is the standard and safest way to swap two values in C++.

Understanding this program helps beginners learn how variables work, how values are stored, and how to modify them. It is a helpful foundation for more advanced logic and algorithms.