Problem overview
The starter code gives the original price of a product and a discount percentage. The program must print the price the customer pays after the discount is taken off.
With a price of 150 and a discount of 10, the discount is 10% of 150, which is 15, so the customer pays 150 - 15 = 135. The starter values are 70 and 15, so the answer to compute is 85% of 70.
Approach
There are two ways to see the same result:
- Work out the discount amount,
price * discount / 100, and subtract it fromprice. - Work out what is left to pay directly. A 15% discount means the customer pays 85% of the price, and 85 is
100 - discount. So the price after the discount isprice * (100 - discount) / 100.
The lesson uses the second way, which needs only one expression.
Step by step
- Compute the share that is still paid.
100 - discountis 85 for a discount of 15. Parentheses are needed here, because without them multiplication would run before the subtraction. - Multiply by
price. 70 times 85 is 5950. - Divide by 100 to turn the percentage back into a fraction: 5950 divided by 100 is 59.5.
- Print the expression. No variable is needed for it.
The solution in Swift
let price = 70.0
let discount = 15.0
print(price * (100 - discount) / 100)
The output is:
59.5
The Swift details worth noticing:
- The type comes from the literal. A number written with a decimal point, such as
70.0, makes Swift inferDouble. Writing70would inferInt. - Literals adapt in the expression. The
100in100 - discountand in/ 100becomes aDoublebecause the values next to it areDouble. No conversion is written. - Typed values don't mix. If
discountwere anIntconstant andpriceaDouble,price * (100 - discount)wouldn't compile, because the parentheses would hold anInt. Convert explicitly withDouble(discount). - Output format. A
Doubleprints with a decimal part, so a whole result such as the task's example, 150 and 10, prints as135.0.
Edge cases and common mistakes
- Missing parentheses.
price * 100 - discount / 100multiplies and divides first, and gives 7000 - 0.15 = 6999.85. - Subtracting the percentage directly.
price - discounttakes 15 dollars off, not 15 percent. - Using integers. With
Intconstants, results that need a fraction are cut off. For a price of 70 and a discount of 15,price * (100 - discount) / 100evaluates to 5950 / 100, which is 59, not 59.5. The decimal point in the starter code prevents that. - Printing the discount amount.
price * discount / 100is the previous exercise's answer: 10.5. This task wants the amount that remains. - A 0% or 100% discount. At 0 the price stays at 70.0, and at 100 the result is 0.0. The formula covers both without a special case.
Testing
price = 150.0,discount = 10.0prints135.0.price = 70.0,discount = 15.0prints59.5.price = 80.0,discount = 0.0prints80.0.
Key takeaways
- Paying after a discount of
dpercent means paying100 - dpercent of the price. - Parentheses make the subtraction happen before the multiplication.
- A decimal point in a literal makes it a
Double, which keeps fractions.
