Start learning free

LESSON 23 OF 347 · CALCULATIONS WITH NUMBERS

Free Coffee Cups in C#: Counting Groups with 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

    C# · CALCULATIONS WITH NUMBERS

    Free Coffee Cups in C#, explained

    What the exercise asks

    For every 6 cups you pay for, you get a 7th cup free. The variable cups holds the number of cups you paid for (213 in the editor), and you print the total number of cups you receive: the paid cups plus the free ones. For the statement's example, 12 paid cups earn 2 free cups, so the total is 14.

    Counting the free cups

    A free cup is earned for each complete group of 6 paid cups. A partial group earns nothing. So the question is "how many times does 6 fit completely into cups?", and that is exactly what integer division answers.

    Console.WriteLine(12 / 6);   // 2
    Console.WriteLine(13 / 6);   // 2
    Console.WriteLine(5 / 6);    // 0
    Console.WriteLine(213 / 6);  // 35
    

    In C#, dividing two int values gives an int and drops the fractional part. 213 divided by 6 is 35.5 in decimal arithmetic, and the whole part, 35, is the number of full groups. The remaining 3 paid cups form an incomplete group and earn nothing.

    Step by step

    1. Free cups. cups / 6 is 35.
    2. Total cups. Add the free cups to the paid ones: cups + cups / 6, which is 213 + 35 = 248.
    3. Print the total.

    Precedence works in your favour. Division runs before addition, so you don't need parentheses around cups / 6. Adding them, as in cups + (cups / 6), is harmless if it helps you read the expression.

    The solution in C#

    int cups = 213;
    
    Console.WriteLine(cups + cups / 6);
    

    This prints 248.

    Why not use doubles?

    With a double variable the division keeps its fraction: 213 / 6.0 is 35.5, and you would count half a free cup. Integer division is the right tool when the thing you count comes in whole units. If you ever need to round a floating-point value down yourself, Math.Floor does it, but here that would only add noise.

    Reading the problem carefully

    The statement says both "for each 6 cups I buy, I get a 7th free" and "in total, I get 7 cups". A common slip is to treat the group as 7 cups and divide by 7. Work through the example instead: 12 paid cups make two groups of 6, each with a bonus, which is 2 free cups and 14 in all. Dividing by 7 would give only 1 free cup. When the wording is confusing, test your formula against the example in the statement before anything else. It takes seconds and catches this kind of mistake.

    The same check also shows that free cups are not counted again. The 14 cups in the example come from 12 paid and 2 free, and you do not look for further groups inside the 14.

    Edge cases

    • Fewer than 6 cups. With cups = 5, 5 / 6 is 0, so the total is 5. No free cup yet.
    • Exactly 6 cups. 6 / 6 is 1, so the total is 7, matching the story.
    • Zero cups. The total is 0.
    • Free cups do not earn more. The statement counts only paid cups, so the free cups are not fed back into the division.

    Common mistakes

    • Dividing by 7. It is natural to think in groups of 7 cups, but the free cup is earned by 6 paid cups, so the divisor is 6. With 12 paid cups, 12 / 7 gives 1, which is wrong.
    • Forgetting to add the paid cups. cups / 6 alone is only the free cups.
    • Using the remainder. cups % 6 is the leftover paid cups, which is not what the question asks.
    • Rounding up. Integer division never rounds up, and you don't want it to here.

    Testing

    Try small inputs and work out the answers by hand before running them. cups = 6 should print 7. cups = 12 should print 14. cups = 13 should print 15, because 13 paid cups complete two groups. cups = 5 should print 5.

    Key takeaways

    • Integer division counts how many complete groups fit into a number.
    • The fractional part is discarded, not rounded.
    • Total cups is paid cups plus free cups: cups + cups / 6.
    Keep going with C#About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free