Grupo
Funciones en C++
Ojetivo
1. Escriba una función que reciba un año entero como parámetro.
2. Dentro de la función, utilice sentencias if-else para comprobar si el año cumple las reglas de los años bisiestos.
3. Devuelva verdadero si el año es bisiesto; de lo contrario, devuelva falso.
4. En la función principal, solicite al usuario que introduzca un año y llame a la función.
5. Muestre si el año introducido es bisiesto o no.
Cree una función que determine si un año es bisiesto.
Ejemplo de Código C++
Mostrar Código C++
#include <iostream> // Include the iostream library for input and output
using namespace std; // Use the standard namespace
// Function to determine if a year is a leap year
bool isLeapYear(int year) {
// A year is a leap year if it is divisible by 4 and not divisible by 100,
// unless it is also divisible by 400
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true; // It is a leap year
} else {
return false; // It is not a leap year
}
}
// Main function - entry point of the program
int main() {
int year; // Variable to store the input year
// Ask the user to enter a year
cout << "Enter a year: ";
cin >> year; // Read the year from user input
// Call the function to check if it is a leap year
if (isLeapYear(year)) {
// If true, print this message
cout << year << " is a leap year." << endl;
} else {
// If false, print this message
cout << year << " is not a leap year." << endl;
}
return 0; // End of the program
}
Salida
Enter a year: 2024
2024 is a leap year.
Comparte este ejercicio C++