ccompiler.inOnline C Compiler & Docs
C Example Code2026-03-202 min read

Check whether a given year is a leap year in C using Gregorian calendar conditional logic with multiple approaches and test examples.

Leap Year Mathematical Rules

Under the standard Gregorian Calendar, a year has 366 days (leap year) instead of 365 if it satisfies the following condition:

  1. The year must be evenly divisible by 4 (year % 4 == 0),
  2. Except if it is divisible by 100 (year % 100 == 0), in which case it is NOT a leap year,
  3. Unless it is also divisible by 400 (year % 400 == 0), in which case it IS a leap year.

Complete C Implementation

c (ISO Standard)
#include <stdio.h>
#include <stdbool.h>

bool isLeapYear(int year) {
    /* Rule: Divisible by 400 OR (divisible by 4 AND NOT divisible by 100) */
    if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0)) {
        return true;
    }
    return false;
}

int main() {
    int testYears[] = {2000, 2024, 2026, 1900, 2400, 2100};
    int count = sizeof(testYears) / sizeof(testYears[0]);

    printf("Gregorian Leap Year Verification in C:\n\n");
    for (int i = 0; i < count; i++) {
        int yr = testYears[i];
        if (isLeapYear(yr)) {
            printf("  Year %d: LEAP YEAR (366 days)\n", yr);
        } else {
            printf("  Year %d: COMMON YEAR (365 days)\n", yr);
        }
    }

    return 0;
}

Sample Output

text (ISO Standard)
Gregorian Leap Year Verification in C:

  Year 2000: LEAP YEAR (366 days)
  Year 2024: LEAP YEAR (366 days)
  Year 2026: COMMON YEAR (365 days)
  Year 1900: COMMON YEAR (365 days)
  Year 2400: LEAP YEAR (366 days)
  Year 2100: COMMON YEAR (365 days)

Complexity Analysis

  • Time Complexity: O(1) — A few conditional modulo evaluations execute in constant time.
  • Space Complexity: O(1) — Zero memory overhead.

Related C Examples & Tutorials