Problem overview
A coffee shop gives a free cup for every 6 cups you buy, so 6 paid cups bring 7 in total. The variable cups holds the number of cups you paid for (213). The task is to print the total number of cups you receive: the paid ones plus the free ones.
The lesson's example: 12 paid cups form two complete groups of 6, so 2 are free, and the total is 14.
Approach
The only difficult part is counting the free cups. One is earned per complete group of 6. Five paid cups earn nothing, and eleven earn just one. That is a division where the leftover is thrown away, which is exactly what integer division does in Kotlin: cups / 6 counts how many times 6 fits into cups completely.
The total is then cups + cups / 6.
Step by step
- Count the full groups.
cups / 6is the number of free cups. Both operands are integers, so the fractional part is dropped: 213 / 6 is 35, not 35.5. - Add the paid cups. The total is the paid cups plus the free ones.
- Print the sum with
println. Division runs before addition, so no parentheses are needed.
The solution in Kotlin
val cups: Int = 213
println(cups + cups / 6)
The program prints 248: 213 paid cups plus 35 free ones. Things to know about this line:
- The type is doing the rounding.
cupsis anIntand so is the literal6, so the quotient is anInt. Withcups / 6.0the result would be aDouble, 35.5, and the sum would be 248.5, which is not a number of cups. - The remainder is ignored on purpose. 213 is 35 full groups and 3 leftover cups. Those three earn nothing.
- The free cups are not counted again. A free cup does not count toward the next group. The lesson counts free cups only from paid ones.
Why integer division fits this problem
Many real counting questions are "how many complete groups?", and integer division answers exactly that. The remainder of 3 cups is not lost by accident; it is simply not enough for another group. If you also wanted to know how many paid cups are still needed for the next free cup, the remainder operator gives it: 6 - cups % 6 is 3 for 213 cups.
Here the same idea is used once. The quotient is the number of free cups, and adding it to cups gives the total.
Edge cases and common mistakes
- Fewer than 6 cups. With
cups = 5, the division gives 0 and the total is 5. - Exactly 6 cups. The total is 7: the first free cup is earned on the sixth paid one, not the seventh.
- Dividing by 7. It is natural to think about the "7th cup", but groups are counted in paid cups, so the divisor is 6.
- Multiplying instead of dividing.
cups * 7 / 6gives 248 for 213 as well, but it is a different formula: for 12 it gives 14, and for 5 it gives 5, but for 7 it gives 8, while the shop gives only 7. - Printing only the free cups. The task asks for the total.
Testing
cups = 12prints14.cups = 5prints5.cups = 6prints7.cups = 213prints248.
Key takeaways
- "How many complete groups" is integer division.
- The leftover that doesn't fill a group is dropped automatically.
- Keep both operands as
Intso the quotient stays a whole number.