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 %, and / between two ints throws away the remainder
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 the fact that / drops the remainder rather than rounding it is a piece of C's behaviour every program you write from here on has to respect.
Go straight to the code ↓Arithmetic, and two kinds of division
C does sums with symbols close to the ones on paper:
+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 hands you a choice you do not get on paper, and that choice is what this exercise
is about.
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 numbers are int, C gives you an operator for each:
/counts the whole groups, truncated toward zero, so17 / 5is3%reports what is left over, so17 % 5is2
% has nothing to do with percentages here; it means remainder, the part that would not fit. Both operators
hand back plain whole numbers, which is exactly what you want when the things being counted cannot sensibly
be cut in half — apples, chairs, boxes, pizzas.
A worked example
#include <stdio.h>
int main(void) {
int apples = 17;
int friends = 5;
int each = apples / friends;
int over = apples % friends;
printf("Apples each .. %d\n", each);
printf("Left in bowl . %d\n", over);
return 0;
}Running that prints two lines:
Apples each .. 3
Left in bowl . 2Two operators, the same pair of numbers, two different and equally true answers. The rows of dots are nothing clever — they are part of the text inside the quotes, doing the job of the 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 number of slices wanted, and it already prints the whole summary block. Two lines are unfinished:
int pizzas = 0;
int extra = 0;Replace each 0 with a sum 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 been 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 the ninth pizza and have three slices going spare.
If something goes wrong
C will not warn you that 69 / 8 throws away a remainder — that is simply what / does when both sides are
int. If you want the fractional answer instead, 8.625, you would need at least one side to be a
double, which the variables-and-types lesson covers; a pizza count has no use for it here.
The other thing worth watching is the two operators landing the wrong way round. Nothing breaks 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.
Press Esc then Tab to move keyboard focus out of the code editor.
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
- Expecting / to give a fraction, like 8.625
- guests, slices_each and per_pizza are all declared int, so slices_needed / per_pizza is int arithmetic from the start: 69 / 8 is 8, with the .625 thrown away rather than rounded. Nobody sells you 0.625 of a pizza anyway — this is the answer a pizza order actually wants.
- Putting the two operators the wrong way round
- Nothing breaks and the block still prints, but the numbers stop making sense: extra would print 8, and a leftover of 8 slices is a whole pizza that should have been counted, not left over.
- Deleting the 0 and leaving the line unfinished, as in int pizzas = ;
- A declaration with nothing after the equals sign is not a complete statement. The compiler stops there with a syntax error and points at that exact line.
- Writing per_pizza / slices_needed instead of slices_needed / per_pizza
- Division is not symmetrical: 8 / 69 is 0, because zero whole 69s fit inside 8. Swapping the operands answers a completely different question from the one being asked.
Longer explanation: read the full lesson. Want a blank editor instead? Open the C playground.