Sum of Two Numbers
Program to add two numbers entered by the user
BeginnerTopic: Basic Programs
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;
}Output
Enter first number: 10 Enter second number: 20 Sum of 10 and 20 is: 30
Understanding Sum of Two Numbers
This program demonstrates basic arithmetic operations. We declare three integer variables: num1, num2, and sum. We read two numbers from the user, add them using the + operator, and store the result in sum. Finally, we display the result.
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.