Start learning free

LESSON 24 OF 414 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in Scala: Counting with Int Division

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

    SCALA · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in Scala, explained

    The task

    A coffee shop gives one 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. The exercise is to print the total number of cups you receive, paid and free together. If you paid for 12 cups, you receive 2 free ones, and the total is 14.

    Approach

    The total has two parts:

    • the cups you paid for, which is cups;
    • the free cups, one for each complete group of 6 paid cups.

    The second part is a "how many times does 6 fit into cups" question. That is the quotient of cups divided by 6, and with Int values the operator / computes it. A partial group earns nothing, and integer division drops exactly that partial group. For example, 13 cups hold two complete groups of 6 and one cup left over, so 13 / 6 is 2.

    Step by step

    1. Compute the free cups: cups / 6. With 213 paid cups, 213 / 6 is 35.
    2. Add them to the paid cups: cups + cups / 6, which is 213 + 35 = 248.
    3. Print the sum with println.

    No parentheses are needed. The division runs before the addition, so Scala reads the expression as cups + (cups / 6).

    The solution in Scala

    val cups: Int = 213
    println(cups + cups / 6)
    

    The output is:

    248
    

    Why integer division is the right tool

    The type of cups is Int, and 6 is an Int literal, so cups / 6 is an Int division and has no fractional part. If cups were a Double, then cups / 6 would give 35.5 for 213 paid cups, and you would need to round down yourself. Half a free cup is not something the shop can hand out.

    The cups left over in the last, incomplete group are cups % 6. For 213 that is 3. These cups are paid for, and they stay in the total through the cups + part of the expression. They just do not earn a free cup.

    Edge cases and common mistakes

    • Fewer than 6 cups. With 5 paid cups, 5 / 6 is 0, so the total is 5. No free cup appears early.
    • Exactly 6 cups. 6 + 6 / 6 is 7, as the offer says.
    • Zero cups. The total is 0.
    • Multiplying groups by 7. cups / 6 * 7 counts only complete groups and ignores the leftover paid cups. For 213 it prints 245 instead of 248.
    • Dividing by 7. The free cup is earned per 6 paid cups, so the divisor is 6.
    • Counting the free cups as paid. Free cups do not earn more free cups. The count is based on cups, not on the running total.

    Key takeaways

    • Integer division counts complete groups: cups / 6.
    • The leftover part of an incomplete group is dropped by / and available as %.
    • The total is the paid cups plus the free ones: cups + cups / 6.
    Keep going with ScalaAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free