C Program to Check Leap Year - Nested if/else Logic
Check Whether a Year is a Leap Year
#include <stdio.h>
int main() {
int year;
printf("Enter a year: ");
scanf("%d", &year);
// Leap year if divisible by 400
if (year % 400 == 0) {
printf("%d is a leap year.\n", year);
}
// Not divisible by 400 but divisible by 100 means not a leap year
else if (year % 100 == 0) {
printf("%d is not a leap year.\n", year);
}
// Divisible by 4 means leap year
else if (year % 4 == 0) {
printf("%d is a leap year.\n", year);
}
// All other years are not leap years
else {
printf("%d is not a leap year.\n", year);
}
return 0;
}
Enter a year: 2000
2000 is a leap year.
The leap year rules ensure that our calendar stays aligned with the Earth’s orbit around the sun. A year divisible by 4 is typically a leap year, but century years (divisible by 100) must also be divisible by 400 to qualify. This nested if/else logic captures those rules in a clear way.
⬅ Previous TopicC Program - Print Prime Numbers From 1 to N
Next Topic ⮕C Program - Check Whether a Number is a Palindrome or Not
Next Topic ⮕C Program - Check Whether a Number is a Palindrome or Not
Comments
Loading comments...