Laurand Osmeni

Let's build a faster web.


How do I create a calculation program with C++.

As always first I begin with a empty file named test.cpp.

Declaring integer to store four variables.

int number1, number2, number3, sum;

Then following with cout, in C++ cout prints on the screen, cin is an input, asks user to enter a number or character, as for the endl, stands for end of line.

cout << "Add 10 to the sum" << endl;

I am adding three times cout and cin, because each time should print on the screen and to keep user informed what to do.

At this program i am calculating with addition +, it works the same using others arithmetics, simple change to - substruct, or * multiply, or / to divide.

sum = number1 + number2 + number3;

Watch live in action creating the program, or scroll below to copy the entire code.

C++ Calculation
How do I create simple calculation program with C++.
#include <iostream> using namespace std; // Laurand Osmeni Calcualtion in C++. // Basic C++ Program. int main () { // Storing variables to hold 3 inputs and the sum. int number1, number2, number3, sum; // Print on the screen and ask user to enter the number. cout << "Add 10 to the sum" << endl; cin >> number1; cout << "Add 15 to the sum" << endl; cin >> number2; cout << "Add 20 to the sum" << endl; cin >> number3; // Now calculate the entered numbers and the total sum. sum = number1 + number2 + number3; cout << "The total sum of all entered numbers is. " << sum << endl; return 0; }

Feel free to use the code.

Last update .

Navigate between pages.