- The program first declares a variable
year to store the user input. - It then prompts the user to enter a year using the
printf() and scanf() functions. - An
if-else statement is used to check whether year is a leap year or not. According to the Gregorian calendar, a year is a leap year if it is divisible by 4, but not by 100, unless it is divisible by 400. The if condition checks whether year is divisible by 4 but not by 100, or whether year is divisible by 400. If the condition is true, the program prints that year is a leap year. Otherwise, the program prints that year is not a leap year. - Finally, the program returns 0 to indicate successful execution.
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
printf("%d is a leap year.", year);
} else {
printf("%d is not a leap year.", year);
}
return 0;
}
OUTPUT
Enter a year: 2024
2024 is a leap year.