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.

Sai Kishore Randhi, author
About the author

Sai Kishore Randhi

Embedded systems, VLSI, and chip design

Sai Kishore writes about embedded systems, VLSI, chip design, and the programming concepts behind modern hardware.

View LinkedIn profile ↗