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
Show 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