Swap Two Numbers

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

BeginnerTopic: Basic Programs
Back

C++ Swap Two Numbers Program

This program helps you to learn the fundamental structure and syntax of C++ programming.

Try This Code
#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

Understanding Swap Two Numbers

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

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++.

---

#include <iostream>

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:

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

---

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

4. Showing Values Before Swapping

Before swapping happens, we print the original values:

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

---

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

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:

This confirms that the values have been exchanged successfully.

---

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

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.

Let us now understand every line and the components of the above program.

Note: To write and run C++ programs, you need to set up the local environment on your computer. Refer to the complete article Setting up C++ Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your C++ programs.

Practical Learning Notes for Swap Two Numbers

This C++ program is part of the "Basic Programs" topic and is designed to help you build real problem-solving confidence, not just memorize syntax. Start by understanding the goal of the program in plain language, then trace the logic line by line with a custom input of your own. Once you can predict the output before running the code, your understanding becomes much stronger.

A reliable practice pattern is to run the original version first, then modify only one condition or variable at a time. Observe how that single change affects control flow and output. This deliberate style helps you understand loops, conditions, and data movement much faster than copying full solutions repeatedly.

For interview preparation, explain this solution in three layers: the high-level approach, the step-by-step execution, and the time-space tradeoff. If you can teach these three layers clearly, you are ready to solve close variations of this problem under time pressure.

Table of Contents