Small Steps I — Creating with Text and Numbers
Multiplication and calculation order
Store a width of 3.5 and a height of 2.0 in double variables, calculate the area, and print it.
What you will learn
* means multiplication. Multiplication happens before addition. You can also use parentheses to group the part to calculate first.
Try it
Example · example.cpp
void small_main()
{
print(2 + 3 * 4);
print((2 + 3) * 4);
}
The first line calculates 3 × 4 first. The second calculates 2 + 3 inside parentheses first. Use parentheses to make the order of a complicated expression clear.
Expected output:
14
20
Exercise
Store a width of 3.5 and a height of 2.0 in double variables, calculate the area, and print it.
Exercise starter · exercise_starter.cpp
void small_main()
{
print(2 + 3 * 4);
print((2 + 3) * 4);
}
Hint
Even when the product is 7.0, it may appear as 7 on the screen.
show solution
void small_main()
{
double width = 3.5;
double height = 2.0;
print(width * height);
}