Calculate the Price with Discount Based on Total Purchase in C++

In this exercise, you will create a program that calculates the total price of a purchase, applying a discount based on the total amount. The program will prompt the user to enter the original price of the items in the purchase. Based on the total amount, the program will apply the corresponding discount and then display the final price.

You will use conditional statements to apply different discounts based on the total amount of the purchase. This program will help you practice working with arithmetic operations, user input, and conditional statements in C++.

Group

Advanced Conditional Structures in C++

Objective

1. Prompt the user to enter the total amount of the purchase.
2. Apply a discount based on the total purchase amount:
- If the total is greater than or equal to $100, apply a 20% discount.
- If the total is between $50 and $99.99, apply a 10% discount.
- If the total is below $50, apply a 5% discount.
3. Calculate the final price after applying the discount.
4. Display the original total, discount applied, and final price to the user.

Create a program that calculates the price of a purchase with discounts based on the total.

Example C++ Exercise

 Copy C++ Code
#include <iostream>  // Include input/output stream
#include <iomanip>    // Include iomanip for controlling the output format

using namespace std;

int main() {
    double totalAmount;  // Variable to store the total amount of the purchase
    double finalPrice;   // Variable to store the final price after applying the discount
    double discount;     // Variable to store the discount percentage

    // Prompt the user for the total amount of the purchase
    cout << "Enter the total amount of your purchase: $";
    cin >> totalAmount;  // Get the user's input for total amount

    // Apply the discount based on the total amount
    if (totalAmount >= 100) {
        discount = 0.20;  // Apply 20% discount for purchases above or equal to $100
    } else if (totalAmount >= 50) {
        discount = 0.10;  // Apply 10% discount for purchases between $50 and $99.99
    } else {
        discount = 0.05;  // Apply 5% discount for purchases below $50
    }

    // Calculate the final price
    finalPrice = totalAmount - (totalAmount * discount);  // Subtract discount from total

    // Display the result with proper formatting
    cout << fixed << setprecision(2);  // Set output format to 2 decimal places
    cout << "Original amount: $" << totalAmount << endl;  // Show the original total amount
    cout << "Discount applied: " << discount * 100 << "%" << endl;  // Show the discount percentage
    cout << "Final price after discount: $" << finalPrice << endl;  // Display the final price
    return 0;  // End of the program
}

 Output

Enter the total amount of your purchase: $120
Original amount: $120.00
Discount applied: 20%
Final price after discount: $96.00

Share this C++ Exercise


More C++ Programming Exercises of Advanced Conditional Structures in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.