Start learning free

LESSON 24 OF 348 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in Kotlin

Lesson Read the full explanation

For each of the 6 coffee cups I buy, I get a 7th cup free. In total, I get 7 cups.

Knowing that I paid for cups cups, compute and print the total number of cups I would get (number of cups I paid for + number of cups I got for free).


Example:

For example, if cups was 12, the answer would be 14.

Why? Because I paid for 12 cups and I got 2 for free, which is a total of 14 cups.

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in Kotlin, explained

    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

    1. Count the full groups. cups / 6 is the number of free cups. Both operands are integers, so the fractional part is dropped: 213 / 6 is 35, not 35.5.
    2. Add the paid cups. The total is the paid cups plus the free ones.
    3. 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. cups is an Int and so is the literal 6, so the quotient is an Int. With cups / 6.0 the result would be a Double, 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 / 6 gives 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 = 12 prints 14.
    • cups = 5 prints 5.
    • cups = 6 prints 7.
    • cups = 213 prints 248.

    Key takeaways

    • "How many complete groups" is integer division.
    • The leftover that doesn't fill a group is dropped automatically.
    • Keep both operands as Int so the quotient stays a whole number.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free