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
- Multiply the price by the percentage.
price * discountis70 * 15, which is1050. - Divide by 100. This turns the percentage into a fraction of the price.
1050 / 100is10.5. Because the left side is adouble, the division keeps the decimal part. - 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.
discountis 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 / 100still multiplies first, but the final division drops the fraction and prints10instead of10.5. - Expecting exact decimals. A
doublestores binary approximations, so some results such as0.1 * 3print as0.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
doublewhen a result can have a fractional part. - Integer division discards the fraction; double division keeps it.