Addition in C++ programming language.
January 21, 2023 2023-01-21 15:00Addition in C++ programming language.
Addition in C++ programming language.
Here’s an example of a C++ program that calculates the sum of two numbers:
#include <iostream>
using namespace std; int main() {
int num1, num2, sum;
cout << “Enter two numbers: “;
cin >> num1 >> num2;
sum = num1 + num2;
cout << “The sum is: ” << sum << endl;
return 0;
}
In this program, we first include the iostream library, which allows us to use the input/output functions like cout and cin. Then, we define a main function, which is where the program starts executing.
Inside the main function, we declare three variables: num1, num2, and sum. We use the cout function to display a message asking the user to enter two numbers, and the cin function to read the input from the user.
We then add the two numbers and store the result in the sum variable. Finally, we use the cout function again to display the sum.
You can run this code on any C++ compiler like GCC, CLANG, MSVC. The result of running this code would be to prompt the user to enter two numbers and after entering the numbers it will display the sum of those two numbers.