Grupo
Estructuras Condicionales Avanzadas en C++
Ojetivo
1. Solicite al usuario que ingrese su edad.
2. Use lógica condicional para determinar el precio según las siguientes reglas:
- Edad 0-4: Gratis
- Edad 5-17: $5
- Edad 18-64: $10
- Edad 65 o más: $6
3. Muestre el precio de la entrada correspondiente.
Implemente un programa que calcule el precio de la entrada según la edad del usuario.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the input/output stream library
using namespace std;
int main() {
int age; // Variable to store the user's age
int ticketPrice; // Variable to store the calculated ticket price
// Ask the user to enter their age
cout << "Enter your age: ";
cin >> age;
// Determine the ticket price based on age
if (age >= 0 && age <= 4) {
ticketPrice = 0; // Children under 5 enter for free
} else if (age >= 5 && age <= 17) {
ticketPrice = 5; // Children and teenagers pay $5
} else if (age >= 18 && age <= 64) {
ticketPrice = 10; // Adults pay $10
} else if (age >= 65) {
ticketPrice = 6; // Seniors pay $6
} else {
// Handle invalid age input
cout << "Invalid age entered." << endl;
return 1; // Exit the program with an error code
}
// Output the ticket price
cout << "Your ticket price is: $" << ticketPrice << endl;
return 0; // End of program
}
Salida
Enter your age: 70
Your ticket price is: $6
Comparte este ejercicio C++