Computers are incredible at doing calculations. Now that we have declared variables, let’s use them with arithmetic operators to calculate things!
Here are some arithmetic operators:
+
addition-
subtraction*
multiplication/
division%
modulo (divides and gives the remainder)
For example:
int score = 0; // score is 0 score = 4 + 2; // it is now 6 score = 4 - 2; // it is now 2 score = 4 * 2; // it is now 8 score = 4 / 2; // and now 2 score = 5 % 2; // and now 1
Note: The order of operations can be specified using parentheses. For example, the use of parentheses in score = 5 * (4 + 3)
sets score
equal to 5 * 7
rather than 20 + 3
.
Instructions
In math.cpp, we have already declared and initialized a variable called score
for you.
Let’s change its value by giving it the value of 1234 multiplied by 99.
But how would we know what that value is?
You can output the value by simply adding this code underneath:
std::cout << score << "\n";
Notice how when we want to output a variable, we don’t add double quotes around its name.
Compile and execute your program using the terminal.