Start learning free

LESSON 189 OF 289 · PROJECTS: EVERYDAY PROGRAMS

Shopping Cart V2 in C#: Discount and Tax with Integer Math

Practice Read the full explanation

Add a discount and sales tax to a single-item purchase using integer math.

You start with:

        string item = "Coffee Mug";
        int price = 12;
        int quantity = 3;
        int tax_rate = 8;
        int discount = 5;

Build:

Subtotal: $36
Discount: $5
Tax: $2
Total: $33

Rules:

  • subtotal = price * quantity.
  • after_discount = subtotal - discount (the discount is a flat amount taken off before tax).
  • tax = after_discount * tax_rate / 100 (integer division, so the result is rounded down). The tax is charged on the discounted amount.
  • total = after_discount + tax.
  • Build four lines (Subtotal, Discount, Tax, Total) and join them with a newline "\n" into one string result, then print it with Console.WriteLine(result).

Compute the four numbers (subtotal, after_discount, tax, total) before formatting.

In C# "Total: $" + total converts the number to text for you.

C#'s int / int truncates, so after_discount * tax_rate / 100 already rounds down.

        string item = "Coffee Mug";
        int price = 12;
        int quantity = 3;
        int tax_rate = 8;
        int discount = 5;

        int subtotal = price * quantity;
        int after_discount = subtotal - discount;
        int tax = after_discount * tax_rate / 100;
        int total = after_discount + tax;
        string line1 = "Subtotal: $" + subtotal;
        string line2 = "Discount: $" + discount;
        string line3 = "Tax: $" + tax;
        string line4 = "Total: $" + total;
        string result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
        Console.WriteLine(result);

STEP 1 OF 10
    Ctrl + Enter to check

    C# · PROJECTS: EVERYDAY PROGRAMS

    Shopping Cart V2 in C#, explained

    Problem overview

    This version adds a discount and sales tax to the single-item purchase. The data is item, price, quantity, tax_rate (a percentage, here 8) and discount (a flat amount, here 5). The rules are:

    • subtotal = price * quantity
    • after_discount = subtotal - discount: the flat discount comes off before tax.
    • tax = after_discount * tax_rate / 100: integer division, so it rounds down, and it is charged on the discounted amount.
    • total = after_discount + tax

    Four lines are built (Subtotal, Discount, Tax, Total), joined with newlines into result and printed. For three Coffee Mugs at 12 with a discount of 5 and tax of 8%, the subtotal is 36 and the amount after the discount is 31. The tax is 31 * 8 / 100, which is 248 / 100, which rounds down to 2. The total is 33:

    Subtotal: $36
    Discount: $5
    Tax: $2
    Total: $33

    The first version printed an item line and a total. This one calculates in stages and prints a four-line receipt.

    Approach: one named value per step

    The rules are a chain: each number is computed from the previous ones, so the code is a chain of variables in the same order. Naming every stage makes each line easy to read and check. The order of the operations matters. The tax applies to the amount after the discount, not to the subtotal. With a 40 subtotal, a 5 discount and a 10% rate, taxing the subtotal and then subtracting the discount gives 39, while the rules (discount first, then tax on 35) give 38.

    Percentages with int

    To take a percentage of a whole number, multiply by the rate first and then divide by 100. When both operands of / are int, C# does integer division: it drops the fraction instead of rounding to the nearest value. For 35 at 10%, 35 * 10 / 100 is 350 / 100, and the result is 3, not 3.5. That is the rounding the rules ask for. Writing the same calculation with a double divisor, 35 * 10 / 100.0, gives 3.5, which would not fit in an int variable without a cast.

    Step by step

    1. Create subtotal as price * quantity: the cost before any discount or tax.
    2. Create after_discount as subtotal - discount. The discount comes off first.
    3. Create tax as after_discount * tax_rate / 100. Multiply by the rate, then divide by 100 with integer division.
    4. Create total as after_discount + tax: what the customer pays.
    5. Build the four rows as line1 to line4: each is a label with its dollar sign ("Subtotal: $", "Discount: $", "Tax: $", "Total: $") followed by the matching number. The + converts the int to text.
    6. Join them into result with a newline between each pair of rows. Four rows need three newlines.
    7. Print result with Console.WriteLine.

    The solution in C#

            string item = "Coffee Mug";
            int price = 12;
            int quantity = 3;
            int tax_rate = 8;
            int discount = 5;
    
            int subtotal = price * quantity;
            int after_discount = subtotal - discount;
            int tax = after_discount * tax_rate / 100;
            int total = after_discount + tax;
            string line1 = "Subtotal: $" + subtotal;
            string line2 = "Discount: $" + discount;
            string line3 = "Tax: $" + tax;
            string line4 = "Total: $" + total;
            string result = line1 + "\n" + line2 + "\n" + line3 + "\n" + line4;
            Console.WriteLine(result);
    

    Details worth knowing:

    • Integer division. 31 * 8 / 100 is 2. The expression is evaluated left to right, since * and / have the same rank, so the multiplication happens before the division.
    • Division first loses everything. tax_rate / 100 is 8 / 100, which is 0 in integer division. So after_discount * (tax_rate / 100) is 0 whatever the amount is.
    • No implicit double-to-int conversion. If you mix in a double (for example after_discount * 0.08), the result is a double and assigning it to an int is a compile error until you cast it.
    • Names with underscores. tax_rate and after_discount are valid C# identifiers. The lesson uses those names, so use them as written.
    • The unused item. This version prints no item row, so the compiler warns that item is assigned but never used. It is only a warning, and the program runs.
    • Strings are immutable. Joining with + creates a new string and leaves line1 to line4 unchanged.

    Edge cases and common mistakes

    • No discount. With a price of 10, a quantity of 1, a rate of 8 and a discount of 0, the output is Subtotal 10, Discount 0, Tax 0 (80 / 100 rounds down to 0) and Total 10.
    • Tax that rounds down. A Notebook priced 4 with quantity 10, a rate of 10 and a discount of 5: the subtotal is 40, the amount after the discount is 35 and the tax is 3 (350 / 100). The total is 38.
    • Discount equal to the subtotal. With a subtotal and a discount of 36, the amount after the discount is 0, the tax is 0 and the total is 0. A discount larger than the subtotal would make the amount negative, and this version has no rule against it.
    • Taxing the wrong amount. Computing the tax from subtotal gives a different answer than the rules whenever the discount is not zero.
    • Writing "Tax: $" + tax_rate. That prints the rate, 8, instead of the tax amount.
    • Three newlines, not four. Joining four rows with four newlines leaves a blank line at the end of the receipt.

    Testing

    • Coffee Mug, 12, 3, rate 8, discount 5 gives Subtotal 36, Discount 5, Tax 2, Total 33.
    • Price 10, quantity 1, rate 8, discount 0 gives Subtotal 10, Discount 0, Tax 0, Total 10.
    • Price 4, quantity 10, rate 10, discount 5 gives Subtotal 40, Discount 5, Tax 3, Total 38.
    • Coffee Mug, 12, 3, rate 8, discount 36 gives Subtotal 36, Discount 36, Tax 0, Total 0.
    Keep going with C#1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free