Swap Two Numbers
Beginner-friendly C++ program that shows how to swap two numbers using a temporary variable.
C++ Swap Two Numbers Program
This program helps you to learn the fundamental structure and syntax of C++ programming.
#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;
}Enter first number (a): 5
Enter second number (b): 10
Before swapping: a = 5, b = 10
After swapping: a = 10, b = 5Understanding 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:
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;
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:
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
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.