Start learning free

LESSON 23 OF 338 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in Go: Integer Division in Practice

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

    GO · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in Go, explained

    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 paidfull groups (cups / 6)total received
    505
    617
    718
    12214
    000

    The lesson's exercise

    1. Start from the paid cups. The variable cups already holds them, so it is the first term of the sum.
    2. Add the free cups. They are the number of full groups of 6, which is cups / 6. Because cups is an int and 6 is an untyped constant that becomes an int too, Go divides as integers and drops the remainder.
    3. 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 = 12 gives 12 + 2 = 14, as in the statement.
    • cups = 5 gives 5: no complete group, no free cup.
    • cups = 6 gives 7: one complete group.
    • cups = 0 gives 0.
    • cups = 7 gives 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 / 6 alone is the number of free cups, not the total.
    • Using %. The remainder cups % 6 is the size of the incomplete group. It answers a different question and is not part of the total.
    • Converting to float64 without need. Once the value is a decimal, the division keeps its fraction, and you would have to truncate it again. Keep everything as int.
    • Printing text with the number. The lesson's tests expect just the total.

    Key takeaways

    • "How many full groups of n?" is total / n with 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 / 6 needs no parentheses.
    Keep going with GoAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free