Lesson 3 · 3 of 20
Arithmetic and maths functions · C++
Do maths in C++: integer division and remainder, static_cast, compound operators and the <cmath> functions such as sqrt, pow and round.

With two integers, `/` does integer division: 17 / 5 gives 3, with no decimals, and `%` gives the remainder, 2. That is exactly what you need to share 17 cookies among 5 children.
If either number is a double, the division keeps the decimals: cookies / 5.0 gives 3.4. When both are integer variables, `static_cast<double>(cookies)` converts one of them before dividing.
Compound operators are shortcuts: score += 5 means score = score + 5, score *= 2 multiplies and score++ adds one. The comments show the value after each step: 15, 30 and 31.
`<cmath>` brings the maths functions: std::sqrt (square root, here the hypotenuse of a triangle with sides 3 and 4), std::pow (power), std::round, std::floor and std::ceil (round to nearest, down and up) and std::abs (absolute value).
Finally, the program works out the area of a 30 cm pizza with π·r². C++17 has no standard π constant, so the example defines it with const double pi.
Code · main.cpp
#include <cmath>#include <iostream>int main() { // With two ints, / is integer division and % gives the remainder. int cookies = 17, children = 5; std::cout << "Each child gets " << cookies / children << " cookies\n"; std::cout << "Left over: " << cookies % children << '\n'; // If one side is a double, the result keeps the decimals. std::cout << "Exact share: " << cookies / 5.0 << '\n'; // static_cast converts a value to another type. std::cout << "Average: " << static_cast<double>(cookies) / children << '\n'; int score = 10; // compound operators: += -= *= /=, and ++ score += 5; // 15 score *= 2; // 30 score++; // 31 std::cout << "Score: " << score << '\n'; // <cmath> brings the maths functions. double a = 3.0, b = 4.0; std::cout << "Hypotenuse: " << std::sqrt(a * a + b * b) << '\n'; std::cout << "2 to the 10th: " << std::pow(2, 10) << '\n'; std::cout << "round(7.5): " << std::round(7.5) << ", floor(7.5): " << std::floor(7.5) << ", ceil(7.2): " << std::ceil(7.2) << '\n'; std::cout << "abs(-12.5): " << std::abs(-12.5) << '\n'; // Area of a pizza 30 cm across. const double pi = 3.14159265358979; double radius = 15; std::cout << "Pizza area: " << pi * std::pow(radius, 2) << " cm2\n"; return 0;}Output · c++ -std=c++17 main.cpp -o main && ./main
Each child gets 3 cookies Left over: 2 Exact share: 3.4 Average: 3.4 Score: 31 Hypotenuse: 5 2 to the 10th: 1024 round(7.5): 8, floor(7.5): 7, ceil(7.2): 8 abs(-12.5): 12.5 Pizza area: 706.858 cm2
Key points
- Between integers,
/drops the decimals and%gives the remainder. - With a
doublein the operation, the result has decimals. static_cast<double>(x)converts a value to another type.+=,-=,*=,/=and++are shortcuts.<cmath>:sqrt,pow,round,floor,ceil,abs.
Exercise
Work out how many boxes of 12 eggs you can fill with 150 eggs, how many eggs are left over and how many boxes you need to store them all (hint: std::ceil(150 / 12.0)).