Grupo
Introducción a C++
Ojetivo
1. Declarar una variable entera para almacenar la entrada del usuario.
2. Solicitar al usuario que introduzca un número entero.
3. Usar el operador de módulo (%) para comprobar si el número es divisible entre 2.
4. Si el número es divisible entre 2, escribir que es par.
5. De lo contrario, escribir que es impar.
Leer un número entero y determinar si es par o impar.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the iostream library to enable input and output
using namespace std; // Use the standard namespace
// Main function - entry point of the program
int main() {
int number; // Declare an integer variable to store the user input
// Prompt the user to enter an integer number
cout << "Enter an integer: ";
cin >> number; // Read the number from user input
// Use the modulus operator to check if the number is even or odd
if (number % 2 == 0) {
// If the remainder is 0, the number is even
cout << "The number is even." << endl;
} else {
// Otherwise, the number is odd
cout << "The number is odd." << endl;
}
return 0; // Return 0 to indicate successful execution
}
Salida
Enter an integer: 7
The number is odd.
Comparte este ejercicio C++