Start learning free

LESSON 21 OF 348 · CALCULATIONS WITH NUMBERS

Price After Discount in Kotlin

Lesson Read the full explanation

We created two variables to store the original price of a product in dollars and the discount percentage applied to that price.

Use these two variables and arithmetic operations to compute and print the price after the discount.


Example:

For example, if price was 150 and discount was 10, the answer would be 135.

Why? Because the discount is 10%. 10% of 150 dollars is 15 dollars. And 150 - 15 dollars is 135 dollars.

STEP 1 OF 1
    Ctrl + Enter to check

    KOTLIN · CALCULATIONS WITH NUMBERS

    Price After Discount in Kotlin, explained

    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.

    1. Subtract the discount. Compute the discount amount, price * discount / 100, and subtract it from price.
    2. 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

    1. Find the percentage that remains. 100 - discount is 85.0, the share of the price the customer pays.
    2. 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.
    3. 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.
    4. 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 Double from 70.0 and 15.0. Writing 70 would make an Int instead.
    • Mixing Int and Double in an expression is fine. In 100 - discount, the integer literal 100 is combined with a Double, and the result is a Double.
    • The parentheses are required. Without them, price * 100 - discount / 100 multiplies and divides first and gives 6999.85, which is not a price.
    • The result is exact here, but not always. A Double keeps a binary approximation of decimal numbers, so some results show a tiny error, for example 0.1 + 0.2 prints 0.30000000000000004. For these inputs it prints 59.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 - discount is 55.0, but the discount is 15 percent and not 15 dollars.
    • Dividing before the subtraction. price * 100 - discount / 100 is a precedence mistake; keep 100 - discount together in parentheses.
    • Integer division. If price and discount were Int values, price * (100 - discount) / 100 would be 59, since the .5 is dropped. The Double variables in this lesson avoid that.
    • Dividing the discount first with integers. With Int values, discount / 100 is 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.0 prints 135.0.
    • price = 70.0, discount = 15.0 prints 59.5.
    • price = 80.0, discount = 0.0 prints 80.0.
    • price = 80.0, discount = 100.0 prints 0.0.
    Keep going with KotlinAbout 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free