Sum of Two Numbers
Beginner-friendly C++ program that teaches how to take two numbers from the user, add them, and display the result.
C++ Sum of 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 num1, num2, sum;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
sum = num1 + num2;
cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;
return 0;
}Enter first number: 10
Enter second number: 20
Sum of 10 and 20 is: 30Understanding Sum of Two Numbers
This program teaches you how to take two numbers from the user, add them together, and display the result. It is one of the most common and simplest programs for beginners learning arithmetic operations in C++.
---
1. Header File
We start with:
This header allows us to use:
Without this header, the program cannot perform input and output.
---
#include <iostream>2. using namespace std;
It makes the code simpler to read and write.
---
3. Declaring Variables
Inside the main function, we have:
int num1, num2, sum;
This line creates three integer variables:
The data type int means these variables can hold whole numbers (positive or negative).
---
4. Taking Input From the User
To get input from the user, we use cin.
First:
cin >> num1;
The program prints a message asking for the first number.
The user types a number (like 10), and cin stores it in num1.
The same thing happens for the second number:
cin >> num2;
Now num2 holds the second number typed by the user (like 20).
---
cout << "Enter first number: ";5. Adding the Two Numbers
Next, the program performs the addition:
sum = num1 + num2;
This calculates the total of num1 and num2, and stores the result in sum.
If num1 = 10 and num2 = 20, then:
sum = 30
---
6. Displaying the Result
The final output line is:
This prints a complete message showing the two numbers and their sum.
So the final output becomes:
endl moves the cursor to the next line.
---
cout << "Sum of " << num1 << " and " << num2 << " is: " << sum << endl;7. return 0;
This ends the program and tells the computer that everything ran successfully.
---
Summary
This exercise helps beginners understand variables, input/output, and simple arithmetic in C++. These skills are used in almost every program you write later.
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 Sum of 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.