Problem overview
Two variables describe a sale: price, the original price in dollars (70.0), and discount, a percentage (15.0). The task is to print the price after the discount is applied. In the lesson's example, a price of 150 and a discount of 10 give 135, because 10% of 150 is 15 and 150 minus 15 is 135.
Approach
There are two equivalent ways to think about it.
- Subtract the discount. Compute the discount amount,
price * discount / 100, and subtract it fromprice. - Keep what remains. A 15% discount means you still pay 85% of the price. The fraction you keep is
(100 - discount) / 100, and multiplying the price by it gives the answer in one step.
The lesson uses the second form. It needs one expression, and there is no intermediate value to store or misuse.
Step by step
- Find the percentage that remains.
100 - discountis 85.0, the share of the price the customer pays. - Multiply the price by that percentage.
price * (100 - discount)is 70.0 times 85.0, or 5950.0. The parentheses make Kotlin subtract before it multiplies. - Divide by 100. This turns a percentage into dollars. Multiplication and division have the same precedence, so they run left to right, which is the order needed: first the product, then the division.
- Print the result with
println.
The solution in Kotlin
val price = 70.0
val discount = 15.0
println(price * (100 - discount) / 100)
The program prints 59.5. Some details that matter here:
- The literals decide the type. There is no type annotation, so Kotlin infers
Doublefrom70.0and15.0. Writing70would make anIntinstead. - Mixing
IntandDoublein an expression is fine. In100 - discount, the integer literal 100 is combined with aDouble, and the result is aDouble. - The parentheses are required. Without them,
price * 100 - discount / 100multiplies and divides first and gives 6999.85, which is not a price. - The result is exact here, but not always. A
Doublekeeps a binary approximation of decimal numbers, so some results show a tiny error, for example0.1 + 0.2prints0.30000000000000004. For these inputs it prints59.5.
The other approach gives the same number: price - price * discount / 100 computes 70.0 minus 10.5, which is 59.5.
Why the two approaches agree
Subtracting the discount and keeping the remainder are the same calculation written two ways. Since price - price * discount / 100 factors into price * (1 - discount / 100), and 1 - discount / 100 is (100 - discount) / 100, both reach 59.5 for the lesson's values. The single-step form is shorter and has one place to make a mistake. The two-step form is easier to read when you also need the discount amount for another purpose, such as showing "You save 10.5".
It also helps to sanity-check the result. A price after a discount must be smaller than the original and not negative. Here 59.5 is below 70.0, and 15% is a little under a sixth, which matches 70.0 losing 10.5.
Edge cases and common mistakes
- Subtracting the percentage from the price.
price - discountis 55.0, but the discount is 15 percent and not 15 dollars. - Dividing before the subtraction.
price * 100 - discount / 100is a precedence mistake; keep100 - discounttogether in parentheses. - Integer division. If
priceanddiscountwereIntvalues,price * (100 - discount) / 100would be 59, since the .5 is dropped. TheDoublevariables in this lesson avoid that. - Dividing the discount first with integers. With
Intvalues,discount / 100is 0, and the price would stay unchanged. - Zero and full discounts. A discount of 0.0 returns the original price, and 100.0 returns 0.0.
Testing
price = 150.0,discount = 10.0prints135.0.price = 70.0,discount = 15.0prints59.5.price = 80.0,discount = 0.0prints80.0.price = 80.0,discount = 100.0prints0.0.