Start learning free

LESSON 18 OF 347 · CALCULATIONS WITH NUMBERS

Find Discount in C#: Percentages with double

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 discount amount in dollars.


Example:

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

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

STEP 1 OF 1
    Ctrl + Enter to check

    C# · CALCULATIONS WITH NUMBERS

    Find Discount in C#, explained

    What the exercise asks

    The editor gives you the original price of a product in dollars and a discount given as a percentage. You print the discount amount in dollars. A 10% discount on a price of 150 is 15 dollars; the starting values here are a price of 70 and a discount of 15, so the answer is 10.5.

    From a percentage to dollars

    A percentage is a fraction of 100. "15 percent" means 15 out of every 100, which is 15 / 100, or 0.15. To take that share of a price, multiply the price by the fraction:

    discount amount = price * discount / 100
    

    For the example in the statement, 150 * 10 / 100 is 1500 / 100, which is 15.

    Why the variables are double

    Both variables are declared as double, a floating-point type that can hold fractions. This matters because the answer is often not a whole number: 15% of 70 is 10.5. With int variables, C# would divide whole numbers and silently throw the fractional part away, so the lesson uses double to keep it.

    A double expression stays a double. When one operand is a double and the other an int, C# converts the int automatically, so price * discount / 100 works even though 100 is written as an integer literal.

    Step by step

    1. Multiply the price by the percentage. price * discount is 70 * 15, which is 1050.
    2. Divide by 100. This turns the percentage into a fraction of the price. 1050 / 100 is 10.5. Because the left side is a double, the division keeps the decimal part.
    3. Print the result. Pass the whole expression to Console.WriteLine.

    The operators * and / have the same precedence and run left to right, so no parentheses are needed.

    The solution in C#

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

    This prints 10.5. When a double holds a whole number, Console.WriteLine prints it without a decimal point, so a price of 150 and a discount of 10 prints 15, not 15.0.

    An alternative form

    Dividing first also works for double: price * (discount / 100) gives the same amount here. The order matters only with int operands, where discount / 100 would truncate to 0 and the discount would always come out as zero.

    Checking the formula with another example

    Try a price of 200 and a discount of 25. The expression gives 200 * 25 / 100, that is 5000 / 100, which is 50. That matches intuition: 25 percent is a quarter, and a quarter of 200 is 50. A discount of 0 gives 0, and a discount of 100 gives the whole price. Testing a few easy cases like these is a quick way to catch a wrong formula before you submit.

    Why the lesson prints a bare number

    The checker compares your output with the expected text, so print only the number. Extra words such as "Discount: " would make the output differ even when the value is right. For your own programs you can format output however you like, for example Console.WriteLine($"{price * discount / 100:F2}") prints the amount with exactly two decimal places.

    Common mistakes

    • Printing the percentage as it is. discount is 15 percent, not 15 dollars. You must scale it by the price.
    • Dividing by 10 instead of 100. A percentage is out of 100.
    • Using integers by habit. If you change the types to int, price * discount / 100 still multiplies first, but the final division drops the fraction and prints 10 instead of 10.5.
    • Expecting exact decimals. A double stores binary approximations, so some results such as 0.1 * 3 print as 0.30000000000000004. The numbers in this exercise are exact.

    Reading the result

    The number printed is the discount, not the final price. The price you would pay after the discount is a different calculation, which a related exercise covers. Keep the two ideas apart: one is the part you save, the other is the part you keep.

    Key takeaways

    • A percentage of a value is value * percent / 100.
    • Use double when a result can have a fractional part.
    • Integer division discards the fraction; double division keeps it.
    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