C++
#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
Add Two Numbers in C++
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.
Program Flow
- Read two numbers from user
- Add them using the + operator
- Display the result
Key Concepts
-
Variables: Store the input numbers
-
Input: Using
cinto read user input -
Arithmetic: Using + operator for addition
-
Output: Using
coutto display results
Example
If user enters:
- First number: 15
- Second number: 27
Then:
- Sum = 15 + 27 = 42
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.