What the exercise asks
The editor gives you two double variables: price, the original price of a product, and discount, a percentage. Your job is to compute the price after the discount and print it with cout. With the starter values, price = 70 and discount = 15, the program prints 59.5. With price = 150 and discount = 10, as in the example, it prints 135.
How to calculate a discount
A discount of 10% takes away one tenth of the price. There are two equivalent ways to turn that into arithmetic.
- Subtract the discount amount. The amount is
price * discount / 100. For 150 and 10 that is 15, so you pay150 - 15 = 135. - Keep the remaining percentage. If 10% is taken off, you still pay 90%. That is
price * (100 - discount) / 100, or150 * 90 / 100 = 135.
Both give the same number. The lesson uses the second form because it needs only one expression. A few more results, which are handy for checking your own code:
| price | discount | price after discount |
|---|---|---|
| 150 | 10 | 135 |
| 250 | 40 | 150 |
| 70 | 15 | 59.5 |
| 74.5 | 20 | 59.6 |
| 50 | 0 | 50 |
| 50 | 100 | 0 |
The solution in C++
double price = 70;
double discount = 15;
cout << price * (100 - discount) / 100 << endl;
The line that matters is the last one, and it reads from the inside out:
(100 - discount)is the percentage you still pay. With a discount of 15 it is 85.price * 85scales the price by that percentage, giving 5950./ 100turns the percentage into a fraction, giving 59.5.cout << ... << endlprints the value and ends the line.
The multiplication and the division have equal precedence, and C++ evaluates them from left to right, so price * (100 - discount) / 100 means (price * (100 - discount)) / 100. The parentheses around 100 - discount are what you must not forget, and the next sections show why.
As a complete program, outside the editor, the same code needs a header and a main function:
#include <iostream>
using namespace std;
int main() {
double price = 150;
double discount = 10;
cout << price * (100 - discount) / 100 << endl;
}
This prints 135. When the result is a whole number, cout prints it without a decimal point, even though the value is a double.
Why the variables are double
Integer arithmetic in C++ throws away the fractional part. If both operands of / are integers, the result is an integer: 10 / 100 is 0, while 10 / 100.0 is 0.1. Because price and discount are double, every operation in the expression is done in floating point and keeps its decimals.
Here is what happens with int variables instead, using price 155 and discount 15. The exact answer is 131.75.
int price = 155, discount = 15;
cout << price * (100 - discount) / 100 << endl; // 131
cout << price * ((100 - discount) / 100) << endl; // 0
cout << price * (100 - discount) / 100.0 << endl; // 131.75
The first line truncates 131.75 to 131. The second computes 85 / 100 first, which is 0 in integer arithmetic, so the whole price becomes 0. The third divides by the double literal 100.0, which brings the decimals back. Declaring the variables as double avoids the problem.
Common mistakes
- Missing parentheses. With
price = 150anddiscount = 10,price * 100 - discount / 100prints14999.9, andprice * (100 - discount / 100)prints14985. Multiplication and division run before subtraction, so without parentheses100 - discountis never computed first. - Printing the discount amount.
price * discount / 100is the amount that is taken off, 15 in the example. The exercise wants what remains, 135. - Using the discount as a fraction. The variable holds 10, not 0.1.
price * discountwould multiply by 10. - Integer division. As shown above, a fraction such as
(100 - discount) / 100is0when both sides areint. - Forgetting
endlor the semicolon. The value still prints withoutendl, but the next output starts on the same line.
Controlling how the number is printed
By default cout prints a double with at most 6 significant digits and drops trailing zeros. That is why 59.5 appears as 59.5 and 135 as 135, but it also has side effects. With price = 19.99 and discount = 15 the result prints as 16.9915, and with price = 1234567.89 and discount = 10 it prints as 1.11111e+06.
To always show exactly two decimals, include <iomanip> and use fixed and setprecision:
cout << fixed << setprecision(2) << price * (100 - discount) / 100 << endl;
For 1234567.89 and 10 this prints 1111111.10. Both manipulators stay in effect for later output on the same cout. The exercise itself only asks you to print the value, so the lesson's solution does not use them.
Also keep in mind that a double stores most decimal fractions approximately. For a quick calculation that does not matter, but programs that add up many prices usually store whole cents in an integer instead.
Key takeaways
- The price after a percentage discount is
price * (100 - discount) / 100. - Use
doublefor values that can have decimals. Integer division drops the fraction. - Parentheses control the order of evaluation, and they are what makes
100 - discounthappen first. coutprints 6 significant digits by default. Usefixedandsetprecisionto control the decimals.
