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
- Compute the kept percentage.
100 - discountis85. The parentheses matter here, as explained below. - Multiply by the price.
price * 85is5950. - Divide by 100. That turns the percentage into a share of the price, giving
59.5. - 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 / 100is 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, so70and15would print59rather than59.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
doublewhen the answer can have a fractional part.