Adding two numbers is one of the most basic operations in programming.
In C++, it can be done using the “+” operator. Here is a simple program that adds two numbers and prints the result:
#include <iostream> using namespace std; int main() { int num1, num2, sum; // ask for input cout << "Enter the first number: "; cin >> num1; cout << "Enter the second number: "; cin >> num2; // perform addition sum = num1 + num2; // print result cout << "The sum is: " << sum << endl; return 0; }
First, we declare three variables: num1, num2, and sum. These variables are of type int because we are adding integers.
Next, we use the cout statement to ask the user to input the first number, and then use the cin statement to store that number in the num1 variable.
We repeat this process for the second number, storing it in the num2 variable.
We then use the “+” operator to add num1 and num2, and store the result in the sum variable.
Finally, we use the cout statement to print the result, along with some explanatory text.
This program is very simple, but it demonstrates some of the basic concepts of C++ programming, including variable declaration, input and output using cin and cout, and arithmetic operations using the “+” operator.