Exercise 2 of 10 · Arithmetic operators
Recipe Scaler
What you will make
A program that takes a 4-serving recipe, works out the amount of flour and eggs per serving, and figures out what a doubled batch needs.
The one new idea: Arithmetic on ints with +, -, *, and /, including that integer division truncates instead of rounding (7 / 2 is 3)
Integer division truncating instead of rounding trips up almost every new Go programmer at least once, and spotting where it happens is a core skill for anything that divides quantities.
Go straight to the code ↓Go's arithmetic operators (+, -, *, /) look exactly like the math you already know, with one catch: when both operands are integers, / throws away the remainder instead of rounding. 7 / 2 is 3, not 3.5 and not 4. That single fact shapes how you design programs that split totals into equal shares — the result is always a little less than the mathematically exact answer, and any leftover just vanishes.
Worked example
Here's the same scaling logic applied to a bigger batch:
package main
import "fmt"
func main() {
servings := 5
totalFlourGrams := 400
totalEggs := 15
flourPerServing := totalFlourGrams / servings
eggsPerServing := totalEggs / servings
doubleServings := servings * 2
doubleFlour := flourPerServing * doubleServings
fmt.Println("Recipe scaler (per serving):")
fmt.Printf("Flour: %d grams\n", flourPerServing)
fmt.Printf("Eggs: %d\n", eggsPerServing)
fmt.Printf("Doubled batch serves %d, needs %d grams flour\n", doubleServings, doubleFlour)
}Recipe scaler (per serving):
Flour: 80 grams
Eggs: 3
Doubled batch serves 10, needs 800 grams flourHere 400 / 5 divides evenly, so there's no truncation to notice — but in the exercise below, 250 grams split four ways doesn't divide evenly, and that's exactly the case worth paying attention to.
Your turn
The starter program tries to compute flour and eggs per serving and then figure out what a doubled batch needs, but two of the arithmetic operators don't match what their variable names promise. Read each marked line, decide what operation the comment describes, and swap in the right operator.
If something goes wrong
If your flour-per-serving number looks too big, you probably still have multiplication where division belongs. If the doubled batch numbers look identical to the single batch, check that doubleServings is actually double — a leftover + 2 will only add two servings, not double them. And if you're ever unsure why a division result looks lower than you expected, that's the truncation at work: Go integer division always rounds down toward zero for positive numbers.
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
- Using * when the goal is actually to split a total into equal parts.
- Multiplying a total by the number of servings makes it bigger, not smaller — dividing is what shares it out.
- Assuming int division rounds to the nearest whole number.
- Go truncates toward zero for integer division, so 250 / 4 is 62, not 63, even though 62.5 rounds up.
- Writing servings + 2 to mean "twice as many servings".
- Adding 2 only works if the starting count happens to be 2; multiplying by 2 is what doubling actually means for any starting value.
- Forgetting that %d expects an int, not a float.
- If any of these variables were declared as float64 instead of int, %d would print a runtime formatting error rather than a number.
Want a blank editor instead? Open the Go playground.