Grupo
Estructuras Condicionales Avanzadas en C++
Ojetivo
1. Solicite al usuario que ingrese el importe total de la compra.
2. Aplique un descuento basado en el importe total de la compra:
- Si el total es mayor o igual a $100, aplique un descuento del 20%.
- Si el total está entre $50 y $99.99, aplique un descuento del 10%.
- Si el total es menor a $50, aplique un descuento del 5%.
3. Calcule el precio final después de aplicar el descuento.
4. Muestre al usuario el total original, el descuento aplicado y el precio final.
Cree un programa que calcule el precio de una compra con descuentos según el total.
Ejemplo de Código C++
Mostrar Código C++
#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
}
Salida
Enter the total amount of your purchase: $120
Original amount: $120.00
Discount applied: 20%
Final price after discount: $96.00
Comparte este ejercicio C++