Start learning free

LESSON 25 OF 414 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in Rust: Integer Division in Action

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

    RUST · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in Rust, explained

    Problem overview

    A coffee shop gives one free cup for every 6 cups you buy, so each full group of 6 paid cups becomes 7 cups in total. The variable 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.

    For cups = 12 there are two full groups of 6, so 2 cups are free and the total is 14. In the editor cups is 213.

    Approach

    The only question is how many free cups there are, which is the number of complete groups of 6 in cups. That is exactly what integer division computes. Dividing two integers in Rust keeps the quotient and drops the remainder, so cups / 6 counts the full groups and ignores a partial group, which earns nothing.

    213 / 6 = 35 (35 * 6 = 210, remainder 3)
    total = 213 + 35 = 248
    

    The three cups left over don't make a group, so they earn no free cup, and the integer division handles that without any extra code.

    Step by step

    1. Count the free cups with cups / 6. Both operands are integers, so the fractional part is dropped.
    2. Add the paid cups: cups + cups / 6. Division runs before addition, so no parentheses are needed.
    3. Print the sum with println!. The format string "{}" comes first, and each {} in it is replaced by the next argument.

    The solution in Rust

    let cups: i32 = 213;
    
    println!("{}", cups + cups / 6);
    

    This prints 248. Checking the example from the problem: with 12 cups, 12 + 12 / 6 is 12 + 2, which is 14.

    Why integer division is the right tool

    The type of cups is i32, and the literal 6 is read as an i32 too, so / is an integer division. If cups were an f64, then cups / 6.0 would give 35.5 for 213, and the result would be a float, with a fractional number of free cups that makes no sense. Whole items call for integers.

    Quotient and remainder together

    Every number of cups splits into full groups and leftovers. For 213 paid cups, 213 / 6 is the 35 full groups and 213 % 6 is the 3 cups left over. Only the quotient matters for this problem, but the pair is a good way to see what happened: 35 * 6 + 3 gives back 213. Each group of 6 paid cups plus its free cup makes 7 cups, so the 248 cups received are 35 sets of 7, which is 245, plus the 3 leftover cups. Counting the answer this way gives the same number and is a useful cross-check on the formula.

    The free cups themselves never earn more free cups. The offer counts only the cups that were paid for, which is why the formula divides cups, not the running total.

    Edge cases and common mistakes

    • Fewer than 6 cups. With cups = 5, 5 / 6 is 0, so there are no free cups and the total is 5.
    • Exactly 6 cups. 6 / 6 is 1, so the total is 7, which matches the offer.
    • Zero cups. The total is 0, with no error.
    • Dividing by 7. The group that earns a free cup is 6 paid cups, not 7 cups in total. cups / 7 would undercount: for 12 it gives 1.
    • Adding the wrong thing. cups / 6 alone is the number of free cups; the total must add the paid ones too.
    • Counting the remainder. A rounding-up approach would give an extra cup for 3 cups left over. The offer applies only to complete groups.

    Testing

    • cups = 12 prints 14.
    • cups = 213 prints 248.
    • cups = 5 prints 5.
    • cups = 6 prints 7.
    • cups = 0 prints 0.
    Keep going with RustAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free