Start learning free

LESSON 20 OF 409 · CALCULATIONS WITH NUMBERS

Price After Discount in C++: Percentages with double

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

    C++ · CALCULATIONS WITH NUMBERS

    Price After Discount in C++, explained

    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.

    1. Subtract the discount amount. The amount is price * discount / 100. For 150 and 10 that is 15, so you pay 150 - 15 = 135.
    2. Keep the remaining percentage. If 10% is taken off, you still pay 90%. That is price * (100 - discount) / 100, or 150 * 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:

    pricediscountprice after discount
    15010135
    25040150
    701559.5
    74.52059.6
    50050
    501000

    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:

    1. (100 - discount) is the percentage you still pay. With a discount of 15 it is 85.
    2. price * 85 scales the price by that percentage, giving 5950.
    3. / 100 turns the percentage into a fraction, giving 59.5.
    4. cout << ... << endl prints 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 = 150 and discount = 10, price * 100 - discount / 100 prints 14999.9, and price * (100 - discount / 100) prints 14985. Multiplication and division run before subtraction, so without parentheses 100 - discount is never computed first.
    • Printing the discount amount. price * discount / 100 is 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 * discount would multiply by 10.
    • Integer division. As shown above, a fraction such as (100 - discount) / 100 is 0 when both sides are int.
    • Forgetting endl or the semicolon. The value still prints without endl, 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 double for values that can have decimals. Integer division drops the fraction.
    • Parentheses control the order of evaluation, and they are what makes 100 - discount happen first.
    • cout prints 6 significant digits by default. Use fixed and setprecision to control the decimals.
    Keep going with C++About 340 step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free