Problem overview
Two constants are given: price, the original price of a product in dollars, and discount, a percentage. The program must print the discount amount in dollars, not the price after the discount.
With price equal to 150 and discount equal to 10, the discount is 10% of 150, which is 15 dollars. In the starter code the values are 70 and 15, so the program has to work out 15% of 70.
Approach
A percentage is a number out of 100. Taking discount percent of price means multiplying the two and dividing by 100: price * discount / 100. Multiplication and division share a precedence level and run from left to right, so the product is computed first and then divided.
The starter code declares both constants with an explicit Double type. That choice is deliberate: prices and percentages often aren't whole numbers, and 15% of 70 is 10.5.
Step by step
- Keep the two
Doubleconstants. The type annotation: Doublemakes the number 70 a floating-point value. - Multiply
pricebydiscount. This gives the percentage scaled by 100: 70 times 15 is 1050. - Divide by 100. The literal
100takes the type of the other operand, so Swift treats it as aDoubleand keeps the decimal part. 1050 divided by 100 is 10.5. - Pass the expression to
print. No variable is needed for the result.
The solution in Swift
let price: Double = 70
let discount: Double = 15
print(price * discount / 100)
The output is:
10.5
Swift details that matter here:
- A literal adapts to its context. The
100in/ 100is not anIntbecause it appears next to aDouble. Swift infers its type from the other operand. That is why no conversion is needed. - No implicit conversion between variables. Literals adapt, but typed values don't. If
discountwere anIntconstant andpriceaDouble,price * discountwould fail to compile withbinary operator '*' cannot be applied to operands of type 'Double' and 'Int'. Writeprice * Double(discount)in that case. Doubleoutput. ADoublealways prints with a decimal point, so the example from the task, 150 and 10, prints15.0rather than15.
Edge cases and common mistakes
- Integer division. If both constants were
Int, the division by 100 would drop the fraction. Withprice = 7anddiscount = 10as integers,price * discount / 100gives0, although the real discount is 0.7. UsingDoubleavoids the loss. - Dividing first.
price * (discount / 100)is mathematically the same, and withDoublevalues it works. With integers,discount / 100would already be0for any discount under 100. - Printing the price after the discount. This task asks for the discount amount only. Subtracting it from
priceis the next exercise. - Forgetting the division by 100.
price * discountprints 1050.0, which is a percentage-times-dollars value that isn't a dollar amount. - Writing
%for percent. In Swift,%is the remainder operator and doesn't work onDoublevalues at all, so it can't stand in for a percentage sign.
Testing
price = 150,discount = 10prints15.0.price = 70,discount = 15prints10.5.price = 100,discount = 0prints0.0.
Key takeaways
- A percentage of a value is
value * percent / 100. - Use
Doublewhen the result may have a fraction. - Literals adapt to the type of the other operand; typed constants don't convert on their own.
