Grupo
Estructuras Condicionales Avanzadas en C++
Ojetivo
1. Usa el generador de números aleatorios para simular el lanzamiento de un dado de seis caras.
2. Muestra el número obtenido.
3. Determina si el número es par o impar usando el operador módulo.
4. Imprime un mensaje indicando si el número es par o impar.
Crea un programa que simule el lanzamiento de un dado y determine si el número es par o impar.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the input/output stream library
#include <cstdlib> // Include the C standard library for rand() and srand()
#include <ctime> // Include the C time library to seed the random number generator
using namespace std;
int main() {
// Seed the random number generator with the current time
srand(time(0));
// Generate a random number between 1 and 6 (simulating a die roll)
int dieRoll = rand() % 6 + 1;
// Display the result of the die roll
cout << "You rolled a " << dieRoll << "." << endl;
// Check if the number is even or odd
if (dieRoll % 2 == 0) {
// Output message for even number
cout << "The number is even." << endl;
} else {
// Output message for odd number
cout << "The number is odd." << endl;
}
return 0; // End of program
}
Salida
You rolled a 4.
The number is even.
Comparte este ejercicio C++