C Program to find whether the given year is leap or not.

A leap year has 366 days instead of 365, with February 29 as the extra day. The Gregorian calendar uses a four-part rule to decide whether a year is a leap year. This rule is one of the most commonly tested C interview questions because it requires a compound conditional with exactly the right operator precedence.

The Four-Rule Leap Year Logic

Condition Leap Year? Example
Not divisible by 4 No 2023 → 365 days
Divisible by 4, not by 100 Yes 2024 → 366 days
Divisible by 100, not by 400 No 1900 → 365 days
Divisible by 400 Yes 2000 → 366 days

Combined into one expression: (year%4==0 && year%100!=0) || (year%400==0)

C Program: Leap Year Check

/* Check if a year is a leap year
 * Leap year rules:
 *   1. Divisible by 4    → leap year candidate
 *   2. Divisible by 100  → NOT a leap year (century exception)
 *   3. Divisible by 400  → IS a leap year (400-year exception)
 * Combined: (y%4==0 && y%100!=0) || (y%400==0)
 * Compile: gcc -ansi -Wall -Wextra leapyear.c -o leapyear */
#include <stdio.h>

int is_leap(int y)
{
    return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}

int main(void)
{
    int year;
    printf("Enter a year: ");
    if (scanf("%d", &year) != 1 || year < 1) {
        printf("Enter a positive year.\n");
        return 1;
    }
    if (is_leap(year))
        printf("%d is a leap year (366 days).\n", year);
    else
        printf("%d is NOT a leap year (365 days).\n", year);
    return 0;
}

How to Compile and Run

gcc -ansi -Wall -Wextra leapyear.c -o leapyear
./leapyear

Sample Output

Enter a year: 2024
2024 is a leap year (366 days).

Enter a year: 1900
1900 is NOT a leap year (365 days).

Enter a year: 2000
2000 is a leap year (366 days).

Enter a year: 2023
2023 is NOT a leap year (365 days).

Why 1900 is Not a Leap Year but 2000 Is

1900 is divisible by 4 and by 100, but not by 400. So the century exception kicks in: 1900 is NOT a leap year. 2000 is divisible by 400, so the 400-year override kicks in: 2000 IS a leap year. This is why software that assumed 1900 was a leap year (including some early Excel versions) had date bugs.

Code Explanation

  • The expression (y%4==0 && y%100!=0) || (y%400==0) — first checks if divisible by 4 but not 100 (normal leap year), OR divisible by 400 (century override). The parentheses make precedence explicit. Without parentheses, && binds tighter than ||, so the expression is naturally correct — but explicit parentheses are better style for this compound condition.
  • Equivalent step-by-step form — some beginners prefer nested if-else:
    if (y % 400 == 0)          return 1;
    else if (y % 100 == 0)     return 0;
    else if (y % 4  == 0)      return 1;
    else                        return 0;

    Both forms are correct. The single expression is more concise; the nested form is easier to trace step by step.

  • Extracting is_leap() as a function — makes the function independently testable and reusable. Calling is_leap(2000), is_leap(1900) as unit tests is clean. main() reads the year and calls the function — separation of I/O from logic.
  • Real-world use — leap year logic appears in date libraries, calendar applications, and age calculators. In C’s struct tm (from <time.h>), tm_yday goes up to 365 (0-based) for non-leap years and 366 for leap years.

What This Program Teaches

  • Multi-condition boolean expressions — the leap year formula is a classic exercise in combining &&, ||, and % correctly. Getting the precedence and the NOT-100 exception right separates a precise programmer from one who guesses.
  • Extract logic into named functionsis_leap(y) is more readable than an inline condition in an if-statement. Named functions also enable unit testing: you can call is_leap() with known inputs and check the return value without needing I/O.
  • Historical note — why the century exception? The solar year is not exactly 365.25 days but 365.2422 days. A simple ÷4 rule adds too many leap years. The ÷100 correction removes too many. The ÷400 correction adds back the right amount. The average year length becomes (365 × 303 + 366 × 97) / 400 = 365.2425 days — close enough to drift only ~3 days in 10,000 years.

Related Programs

Recommended book:
The C Programming Language — Kernighan & Ritchie (India) |
(US)
 | 
C Programming: A Modern Approach — K.N. King (India) |
(US)

Practice what you learned: C Aptitude Questions — or try our C Programming Quiz App on Android.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>