Start learning free

LESSON 24 OF 415 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in Swift: Integer 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

    SWIFT · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in Swift, explained

    Problem overview

    A shop gives one free cup for every 6 cups bought. The constant cups holds the number of cups paid for, and the program must print the total number of cups received: the paid ones plus the free ones.

    With 12 paid cups there are two complete groups of 6, so 2 cups are free, and the total is 14. With the starter value, 213 paid cups, the answer is 248.

    Approach

    The only hard part is counting the free cups. One free cup is earned per complete group of 6 paid cups. A partial group earns nothing, so 5 paid cups give no free cup, and 13 paid cups give two, not 2.17.

    That is exactly what integer division does. When both operands are Int, Swift's / drops the fractional part. So cups / 6 is the number of complete groups, and therefore the number of free cups. The total is cups + cups / 6.

    Step by step

    1. Divide cups by 6. For 213 this is 35, because 6 fits into 213 thirty-five times (6 * 35 = 210) with 3 left over, and the leftover is discarded.
    2. Add the paid cups. 213 + 35 is 248. Division runs before addition, so no parentheses are needed.
    3. Print the sum.

    The solution in Swift

    let cups: Int = 213
    
    print(cups + cups / 6)
    

    The output is:

    248
    

    Swift details:

    • Integer division truncates toward zero. For positive counts like these, that is the same as rounding down. The behaviour differs only for negative numbers, which don't occur with a cup count.
    • Int stays Int. The result of cups / 6 is an Int, so it adds to cups directly. Swift refuses to add an Int and a Double without an explicit conversion.
    • Division by zero stops the program. Here the divisor is the literal 6, so it never happens.

    The leftover cups

    The remainder is the part of the division that the quotient throws away. cups % 6 is 3 for 213 cups, which means 3 paid cups sit in an unfinished group, and 3 more purchases would earn the next free cup. The exercise needs only the quotient, but the quotient and the remainder always go together: 213 is 6 * 35 + 3.

    Edge cases and common mistakes

    • Fewer than 6 paid cups. With 5, 5 / 6 is 0, so the total is 5.
    • Exactly 6. The first group is complete: 6 + 1 = 7, matching the task's description.
    • Free cups don't earn more free cups. Only paid cups count toward a group, which is why the division uses cups and not the running total.
    • Rounding up. With 13 paid cups, a rounded-up count would give 3 free cups. Integer division gives 2.
    • Using Double arithmetic. Double(cups) / 6 is 2.1666 for 13 cups, which is not a number of cups.

    Testing

    • cups = 12 prints 14.
    • cups = 213 prints 248.
    • cups = 5 prints 5.
    • cups = 6 prints 7.
    • cups = 0 prints 0.

    Key takeaways

    • Dividing two Int values gives the number of complete groups.
    • "One for every N" is a quotient: count / N.
    • The total is the paid amount plus the quotient.
    Keep going with SwiftAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free