Skip to content

Exercise 3 of 10 · Arithmetic

Party Planner

What you will make

A pizza order summary that works itself out from the guest list — slices needed, whole pizzas, and the slices that spill past them.

The one new idea: C++ does arithmetic with + - * / and %

Counting things into whole groups turns up everywhere: pizzas, pages, boxes, minutes into hours. C++ has an operator for each half of that question, and because every number here is declared int, there is no decimal point to trip over.

Go straight to the code ↓

Arithmetic, and two kinds of division

C++ does sums the way you would on paper, with familiar symbols:

  • + adds
  • - subtracts
  • * multiplies, using a star rather than an x

So guests * slices_each means the guests multiplied by the slices each, worked out the moment that line runs.

Division is where C++ asks you a question you did not get asked on paper, and the answer depends entirely on the types either side of the /.

Picture seventeen apples shared between five friends. There are two sensible answers, not one. The first is "three each" — how many whole apples everybody walks away with. The second is "two left in the bowl" — the ones that could not be shared out evenly.

When both sides of / are int, C++ gives you the first answer and quietly throws the second one away: 17 / 5 is 3, full stop, with no rounding and no decimal point, because an int cannot hold one. % is the operator that keeps what / discarded: 17 % 5 is 2. Together they account for every apple exactly once — three whole apples times five friends, plus two left over, comes back to seventeen.

You may know % as the percent sign from ordinary maths. In C++ it means something different: the remainder after integer division, the part that did not fit evenly. It has nothing to do with percentages here.

A worked example

C++
#include <iostream>

int main() {
    int apples = 17;
    int friends = 5;
    int each = apples / friends;
    int over = apples % friends;
    std::cout << "Apples each .. " << each << std::endl;
    std::cout << "Left in bowl . " << over << std::endl;
}
Output
Apples each .. 3
Left in bowl . 2

Two operators, the same pair of numbers, two different and equally true answers. The rows of dots are nothing clever — they are ordinary characters sitting inside the quotes, doing the job of a ruled line on a paper form.

Your turn

The editor has a party to plan. Twenty-three guests are coming, each one will eat three slices, and a pizza turns up cut into eight. The program already multiplies the guests by the slices to get the total slices wanted, and it already prints the whole summary block. Two lines are unfinished:

C++
int pizzas = 0;
int extra = 0;

Replace each 0 with a calculation built from slices_needed and per_pizza. Give pizzas the number of whole pizzas the order fills, and extra the slices that spill past them.

Then run it and read the block back. If the last two numbers still show 0, nothing has changed yet — the program compiles and runs perfectly happily with zeros in it, it just plans a party with no food.

When it works you get a genuine answer: eight whole pizzas cover sixty-four slices, and five slices are still wanted on top. In real life you would order a ninth pizza and have three slices going spare.

If something goes wrong

If a number turns up with a decimal point where you expected a plain count, one of the values involved has quietly become a double somewhere — check that per_pizza and slices_needed are still declared int, because dividing even one double into the sum switches the whole calculation to decimal division.

The other thing worth watching is the two operators landing the wrong way round. Nothing fails to build and the block prints as neatly as ever, but the figures stop hanging together. Leftover slices must always come to fewer than eight — if that line reads 8 or more, there is another whole pizza hiding inside it.

Change it, run it, change it again. Nothing here can break.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
Output will appear here after you run your code.

The runtime is starting in the background. You can type now — it will be ready before you are.

The answer appears here once you have run your code at least once.

Things that often go wrong here

Declaring pizzas or extra as double instead of int
double slices_needed / per_pizza would still divide two ints and give a whole number, because the division happens before it is ever stored — the type of the variable receiving an answer does not change how that answer was worked out. The real risk runs the other way: making per_pizza a double turns the whole division into decimal division, and 69 / 8.0 prints 8.625, which is a correct answer to a maths question and a useless one for ordering pizza.
Putting the two operators the wrong way round
Nothing fails to compile, and the block still prints, but the numbers stop making sense: 5 pizzas cannot feed 23 people, and leftover slices should never reach 8, because 8 slices is a whole pizza.
Deleting the 0 and leaving the line unfinished
A line reading int pizzas = ; is not a complete statement. The compiler stops before building anything and points at that exact line with error: expected expression.

Want a blank editor instead? Open the C++ playground.