The task
A coffee shop gives one free cup for every 6 cups you buy. The program receives cups, the number of cups paid for, and must print the total number of cups received: the paid ones plus the free ones.
In the example, cups is 12. Twelve paid cups make two complete groups of 6, which earn 2 free cups, so the total is 14. The lesson's variable holds 213.
Counting full groups
The question to answer is: how many complete groups of 6 are in cups? That is exactly what integer division computes. When you divide two integers in Go, the result is the number of times the divisor fits completely, and the fractional part is dropped:
cups := 213
fmt.Println(cups / 6) // 35
fmt.Println(cups % 6) // 3
There are 35 full groups, and 3 cups are left over, which is a partial group. A partial group earns nothing, so the remainder does not matter for the answer. Truncating the division is not a loss of information here; it is the behaviour the task needs.
Try it on small values to see the pattern:
| cups paid | full groups (cups / 6) | total received |
|---|---|---|
| 5 | 0 | 5 |
| 6 | 1 | 7 |
| 7 | 1 | 8 |
| 12 | 2 | 14 |
| 0 | 0 | 0 |
The lesson's exercise
- Start from the paid cups. The variable
cupsalready holds them, so it is the first term of the sum. - Add the free cups. They are the number of full groups of 6, which is
cups / 6. Becausecupsis anintand 6 is an untyped constant that becomes aninttoo, Go divides as integers and drops the remainder. - Print the sum with
fmt.Println.
var cups int = 213
fmt.Println(cups + cups / 6)
This prints 248: 213 paid cups plus 35 free ones.
No parentheses are needed. Division ranks above addition, so Go evaluates cups / 6 first and then adds cups to it. Writing cups + (cups / 6) is also correct and some people find it clearer. Writing (cups + cups) / 6 would be a different calculation.
A trap: dividing as decimals
If cups were a float64, cups / 6 would return a decimal such as 35.5, and the total would not be a whole number of cups. The task needs whole groups, so keeping the variable an int is what makes the division do the right thing.
Check with the examples
A formula like this is easy to get wrong by one group, so test the edges: just below, exactly on and just above a multiple of 6.
Running the formula on a few inputs:
cups = 12gives 12 + 2 = 14, as in the statement.cups = 5gives 5: no complete group, no free cup.cups = 6gives 7: one complete group.cups = 0gives 0.cups = 7gives 8: the seventh cup is a partial group and adds nothing.
Common mistakes and gotchas
- Dividing by 7. The free cup is the seventh one, but groups are counted among the cups you pay for, so you divide the paid cups by 6.
- Adding only the free cups.
cups / 6alone is the number of free cups, not the total. - Using
%. The remaindercups % 6is the size of the incomplete group. It answers a different question and is not part of the total. - Converting to
float64without need. Once the value is a decimal, the division keeps its fraction, and you would have to truncate it again. Keep everything asint. - Printing text with the number. The lesson's tests expect just the total.
Key takeaways
- "How many full groups of
n?" istotal / nwith integers. - Integer division drops the remainder, which is exactly right when partial groups earn nothing.
- The remainder from
%tells you how many items are left over. - Division binds tighter than addition, so
cups + cups / 6needs no parentheses.