Start learning free

LESSON 20 OF 347 · CALCULATIONS WITH NUMBERS

Price After Discount in C#: Subtracting a Percentage

Lesson Read the full explanation

We created two variables to store the original price of a product in dollars and the discount percentage applied to that price.

Use these two variables and arithmetic operations to compute and print the price after the discount.


Example:

For example, if price was 150 and discount was 10, the answer would be 135.

Why? Because the discount is 10%. 10% of 150 dollars is 15 dollars. And 150 - 15 dollars is 135 dollars.

STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Price After Discount in C#, explained

    What the exercise asks

    The editor declares two double variables: the original price in dollars (70) and a discount percentage (15). You print the price after the discount is applied. For the statement's example, a price of 150 with a 10% discount, the discount is 15 dollars and the customer pays 135. For the starting values, the answer is 59.5.

    Two ways to think about it

    Subtract the discount. Find the discount amount, price * discount / 100, and take it off the price: price - price * discount / 100. This follows the story, but it names price twice.

    Keep what remains. A 15% discount means you still pay 85% of the price. That share is 100 - discount percent, so the final price is price * (100 - discount) / 100. It uses each variable once and needs one fewer step in your head. The lesson uses this form.

    Both give the same value mathematically. For the starting numbers, the first is 70 - 10.5 and the second is 70 * 85 / 100; each is 59.5.

    Step by step

    1. Compute the kept percentage. 100 - discount is 85. The parentheses matter here, as explained below.
    2. Multiply by the price. price * 85 is 5950.
    3. Divide by 100. That turns the percentage into a share of the price, giving 59.5.
    4. Print it with Console.WriteLine.

    The subtraction form in code

    If you prefer to follow the story, store the discount amount in a variable first and subtract it:

    double saved = price * discount / 100;
    Console.WriteLine(price - saved);  // 59.5
    

    Naming the intermediate value makes the code easy to read, and it is a good habit when a formula has several parts. The one-line version is shorter, and both pass the same tests here.

    Why the parentheses are needed

    Multiplication and division run before subtraction. Without parentheses, price * 100 - discount / 100 would compute price * 100, then discount / 100, then subtract the second from the first, which is a very different number. Wrapping (100 - discount) forces the subtraction first. Then * and /, which have equal precedence, run left to right.

    The solution in C#

    double price = 70;
    double discount = 15;
    
    Console.WriteLine(price * (100 - discount) / 100);
    

    This prints 59.5. Because price is a double, every step stays in floating point and the fractional part of the division is kept. No cast is needed.

    A detail about double

    A double stores binary approximations of decimal numbers, so not every decimal result is exact. The values in this lesson (59.5 and 135) are, but a different price can print a long tail of digits such as 0.30000000000000004. For real money, C# has a decimal type designed to avoid this, though the lesson uses double for simplicity.

    Where this fits

    This exercise combines three ideas from the chapter: arithmetic on variables, precedence with parentheses, and the difference between integer and floating-point division. If the output is wrong, check them in that order. First confirm the formula on paper, then confirm the parentheses put the subtraction where you want it, and finally confirm that the variables are double, so the division keeps its fraction. The same pattern, a percentage turned into a multiplier, appears whenever a percentage is applied to a number, and the multiplier is the percentage divided by 100.

    Common mistakes

    • Printing the discount instead of the final price. price * discount / 100 is the amount saved, not the amount paid.
    • Forgetting the parentheses around 100 - discount.
    • Switching to int. With integer variables, the last division would drop the fraction, so 70 and 15 would print 59 rather than 59.5.
    • Dividing by 10. A percentage is out of 100.

    Testing

    Check the formula by hand for a few easy inputs. A price of 150 with a discount of 10 gives 135. A price of 200 with a discount of 50 gives 100. A discount of 0 returns the price unchanged, and a discount of 100 gives 0.

    Key takeaways

    • Price after a discount is price * (100 - discount) / 100.
    • Use parentheses when subtraction must happen before multiplication.
    • Use double when the answer can have a fractional part.
    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