Add Two Numbers

Beginner-friendly C++ program that reads two numbers from the user, adds them, and displays the result.

BeginnerTopic: Basic Programs
Back

C++ Add 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 num1, num2;
                      
                      cout << "Enter first number: ";
                      cin >> num1;
                      
                      cout << "Enter second number: ";
                      cin >> num2;
                      
                      int sum = num1 + num2;
                      
                      cout << "Sum of " << num1 << " and " << num2 << " = " << sum << endl;
                      
                      return 0;
                  }
Output
Enter first number: 15
                  Enter second number: 27
                  Sum of 15 and 27 = 42

Understanding Add Two Numbers

This program teaches the most basic arithmetic operation in C++ — adding two numbers. It takes two integer values from the user, performs the addition using the + operator, and prints the result. This program is perfect for beginners because it introduces user input, arithmetic operators, variables, and output formatting.

---

1. Header File

This header allows the program to use:

cin → for taking input
cout → for showing output

Without this, the program cannot interact with the user.

---

#include <iostream>

2. Declaring Variables

The program declares two integer variables:

int num1, num2;

These will store the values entered by the user.

Since we are adding whole numbers, int is the best data type for this example.

---

3. Taking Input

The program asks:

Enter first number:

Enter second number:

cin reads the values typed by the user and stores them in num1 and num2.

Example:

num1 = 15

num2 = 27

---

4. Performing the Addition

The sum is calculated using:

int sum = num1 + num2;

The + operator simply adds the two values and stores the result in the variable sum.

For example:

15 + 27 = 42

So sum = 42

---

5. Displaying the Output

The result is printed as:

Sum of 15 and 27 = 42

---

                  cout allows us to combine text with variables using <<.

6. About "6 Methods" Mentioned in Description

There are multiple ways to add two numbers in C++, such as:

1.Using + operator (what this code shows)
2.Using functions
3.Using pointers
4.Using references
5.Using classes and objects (OOP)
6.Using template functions

But this specific program shows

only the simplest method

— basic addition.

These extra methods can be shown in separate examples or lessons as you expand the course.

---

7. return 0;

This ends the program successfully.

---

Summary

The program reads two numbers.
It adds them using the + operator.
It prints the result.
This is the simplest and most essential arithmetic program for beginners learning C++.

Once students master this, they can easily understand subtraction, multiplication, division, and more advanced programs.

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 Add 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