Check Leap Year in C

Beginner
⏱️ 10 min read
📚 Updated: Aug 2026
🎯 2 Code Examples
🚀 Live Preview
Gregorian

What You’ll Learn

Leap years keep the civil calendar aligned with the tropical year by inserting February 29. This tutorial covers the Gregorian 4 / 100 / 400 rule, a compact C predicate, a live preview, algorithm steps, worked C examples, edge cases, and complexity.

Rule

4 / 100 / 400

Divisible by 4, except most centuries, unless divisible by 400.

Predicate

is_leap_year

One boolean expression with %, &&, and ||.

Century Trap

1900 vs 2000

1900 is common; 2000 is leap because of the 400 rule.

Range Scan

2024–2050

List every leap year in a closed interval with the same helper.

Live Preview

Check year

Test any integer year with the same Gregorian predicate.

Feb 29

366 days

A leap year has 366 days; February gets an extra day.

Introduction

A leap year in the Gregorian calendar is a year where February has 29 days (366 days total). The arithmetic rule keeps the civil calendar close to the tropical year.

In C interviews you usually implement a one-line predicate with the 4 / 100 / 400 rule, then optionally list leap years in a range — and call out century exceptions like 1900 vs 2000.

Why it matters?

It is a classic boolean-logic warm-up: modulo, short-circuit operators, and a real-world rule that trips people who only remember “divisible by 4.”

Key Highlights

4 / 100 / 400

The full Gregorian predicate in one line.

O(1) Test

A few modulo operations — constant time.

Century Cases

1900 fails; 2000 passes the 400 rule.

Reusable Helper

One function powers single checks and range scans.

In short: leap iff (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0).

📝 Problem & Approach

Given an integer year, decide whether it is a Gregorian leap year; optionally list every leap year in a closed interval.

c
/* y = 2024
 * 2024 % 4 == 0 and 2024 % 100 != 0  → leap
 *
 * y = 1900
 * 1900 % 100 == 0 and 1900 % 400 != 0  → common
 *
 * y = 2000
 * 2000 % 400 == 0  → leap
 */

Inputs & Outputs

ItemTypeDescription
yearintCivil year to classify (Example 1 uses 2024).
Range boundsintInclusive interval such as [2024, 2050] (Example 2).
Resultflag / textLeap or not; or a printed list of leap years.

Minimal workflow

Pseudocode
function is_leap_year(y):
    return (y mod 4 = 0 and y mod 100 != 0) or (y mod 400 = 0)

Method comparison

TaskIdeaExtra space
Single checkGregorian boolean predicateO(1)
Range scanCall the same helper for each year in [L, R]O(1)

⚡ Quick Reference

GoalPattern
Leap predicate(y % 4 == 0 && y % 100 != 0) || (y % 400 == 0)
Quadrennial onlyy % 4 == 0 — necessary but not sufficient
Century exceptiony % 100 == 0 needs y % 400 == 0 to stay leap
Range loopfor (y = start; y <= end; ++y)
Classic probesTest 2024, 1900, and 2000

📋 Gregorian vs Julian vs Libraries

Related ideas — interviews almost always want the Gregorian predicate.

Gregorian
4/100/400

Standard civil calendar rule

Julian
y % 4

Leap every four years — no century cut

Date APIs
mktime

Production systems often delegate after validation

Interview tip
1900/2000

Recite both century cases out loud

Context

When This Problem Shows Up

Reach for leap-year logic when calendar math or boolean predicates matter.

  1. Interview warm-ups

    Quick check of %, short-circuit logic, and edge cases.

  2. Calendar UIs

    Decide February’s length when rendering a month grid.

  3. Day-of-year math

    Offsets after February depend on leap status.

  4. Range filters

    List or count leap years in [L, R].

  5. Not a full date API

    Month/day validation and time zones are separate work.

Key benefit: a tiny modular predicate with a memorable real-world rule — and clear interview traps around centuries.

🔮 Live Preview

Enter an integer year and see whether the Gregorian predicate says leap or common.

Try 2000, 1900, or 2021.

Live result
Press “Check leap”.

Examples Gallery

Two complete C programs — classify a single year, and list leap years in [2024, 2050]. Click View Output to reveal sample console results.

📚 Getting Started

Compact predicate for year = 2024.

Example 1 — Single Year: 2024

One boolean return; parentheses mirror the prose rule.

c
#include <stdio.h>

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

int main(void) {
    int year = 2024;

    if (is_leap_year(year)) {
        printf("%d is a leap year.\n", year);
    } else {
        printf("%d is not a leap year.\n", year);
    }

    return 0;
}

How It Works

Short-circuiting && and || match the usual truth table. For 2024, % 4 == 0 and % 100 != 0, so the function returns true.

📈 Practical Patterns

Reuse the same helper across a closed year interval.

Example 2 — Leap Years in [2024, 2050]

Scan each year independently; listing matches the classic reference output.

c
#include <stdio.h>

int is_leap_year(int year) {
    if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
        return 1;
    }
    return 0;
}

int main(void) {
    int start_year = 2024;
    int end_year = 2050;
    int y;

    printf("Leap years in the range %d to %d:\n", start_year, end_year);

    for (y = start_year; y <= end_year; ++y) {
        if (is_leap_year(y)) {
            printf("%d ", y);
        }
    }

    printf("\n");
    return 0;
}

How It Works

Leap years are spaced by 4 until a century boundary; 2100 would break the pattern, but this interval stops at 2050.

🧠 How the Algorithm Decides Leap

1

Quadrennial test

Check year % 4 == 0. If false, the year is common.

% 4
2

Century check

If year % 100 == 0, the century exception may apply.

% 100
3

400 override

Centuries divisible by 400 remain leap years.

% 400
=

Verdict ready

For 2024, % 4 passes and it is not a century — leap.

🔎 Worked Walkthrough — Three Classic Years

Trace the same predicate on a normal leap year, a failed century, and a successful 400-year.

Year% 4% 100% 400Result
2024024Leap
190000300Common
2000000Leap
20211Common

Takeaway: divisibility by 4 is necessary but not sufficient once centuries enter the picture.

Use Cases

Where leap-year thinking shows up beyond the interview prompt.

1. Boolean Logic Practice

Combine modulo tests with short-circuit && / ||.

Example: one-line Gregorian predicate.

2. Calendar Rendering

Choose 28 vs 29 days for February.

Example: month grid for March starts one day later in leap years.

3. Day Counters

Day-of-year and date diffs need leap awareness after Feb.

Example: ordinal day of March 1.

4. Range Filters

List or count leap years in an interval.

Example: all leaps in 2024–2050.

5. Validation Helpers

Reject Feb 29 on common years when parsing dates.

Example: input sanitizers for forms.

6. Related Modular Drills

Pairs with even/odd and other small divisibility checks.

Example: even-number interview page.

Pro Tip: always mention 1900 and 2000 — interviewers use those to catch the incomplete “divisible by 4” answer.

Advantages

Why this approach earns interview points.

  1. 1. Tiny Code Surface

    One expression — easy to write, read, and explain.

  2. 2. O(1) Time

    A fixed number of arithmetic operations per year.

  3. 3. Reusable Helper

    One is_leap_year powers single checks and range scans.

  4. 4. Clear Real-World Rule

    You can justify each clause with calendar history if asked.

Pro Tip: lead with the full rule, then code — do not start from % 4 alone and patch later.

Usage Tips

Small habits that keep leap-year code clean in interviews.

  1. 1. Recite 4 / 100 / 400 First

    State the rule out loud before typing the expression.

  2. 2. Keep Parentheses

    Make the && / || grouping obvious even if precedence would suffice.

  3. 3. Probe Century Years

    Always dry-run 1900 and 2000 on paper.

  4. 4. Guard Range Bounds

    If start > end, swap or reject before scanning.

  5. 5. Stay on Positive Civil Years

    Unless asked, avoid negative years and messy historical numbering.

Pro Tip: dry-run the three classic years (table above) before coding — it locks in both exceptions.

Common Pitfalls

Mistakes that commonly break leap-year solutions in C.

  1. 1. Only Checking % 4

    That is the Julian rule; centuries like 1900 are not leap.

    → Include the 100 and 400 exceptions.

  2. 2. Forgetting the 400 Override

    Rejecting all century years wrongly rejects 2000.

    → Centuries divisible by 400 remain leap.

  3. 3. Operator Precedence Confusion

    Messy expressions without parentheses are hard to defend.

    → Write the grouped form that matches the spoken rule.

  4. 4. Negative Years Blindly

    C’s % follows the dividend’s sign; historical numbering is messy.

    → Stick to positive civil years unless the prompt defines a policy.

  5. 5. Claiming Universal History

    Gregorian adoption dates differ by country.

    → Say you are using the proleptic Gregorian arithmetic rule.

Edge Cases

Check these inputs before calling the solution done.

Happy path

2024

Divisible by 4, not a century — leap.

400

Quadricentennial

2000 and 2400 are leap; 2200 is not.

Century

1900

Divisible by 100 but not 400 — common.

BC

Astronomical year 0

Do not mix astronomical 0 with historical 1 BC without a clear convention.

Range

start > end

Swap bounds or guard the loop to avoid empty or inverted scans.

Types

int width

Typical int is plenty for civil years.

🔄 Sample Values

Known results for common interview inputs.

YearLeap?
2024Yes
2021No
2000Yes
1900No

🎯 Practice Problems

Try these variations to lock in the pattern.

1. Read year from stdin

  • Use scanf and print leap / not leap
  • Include century test cases

2. Count in a range

  • How many leap years in [1900, 2000]?
  • Reuse is_leap_year

3. Days in February

  • Return 28 or 29 from a helper
  • Call the leap predicate once

4. Nested if style

  • Rewrite without a single compound expression
  • Confirm same results on 1900 / 2000 / 2024

Notes

  • Rule. (y%4==0 && y%100!=0) || (y%400==0).
  • 1900 is common; 2000 is leap — memorize both.
  • This page uses proleptic Gregorian arithmetic for integer years, not a full historical calendar.
  • Month/day validation and time zones are separate from the leap predicate.

Quick Takeaway: leap iff divisible by 4, except centuries, unless divisible by 400 — then reuse that helper for range listings.

⏱️ Time and Space Complexity

TaskTimeExtra space
One yearO(1)O(1)
Range [a, b]O(b - a + 1)O(1)

No auxiliary memory is required beyond loop indices.

Wrap Up

🎉 Conclusion

Leap year checks are a short modular-arithmetic exercise with clear interview payoff: the full Gregorian rule, century traps, and a reusable helper for range scans.

Practice the two examples above, then continue to magic number for another digit-property warm-up.

Remember (y%4==0 && y%100!=0) || (y%400==0) — and always probe 1900 vs 2000.

💡 Best Practices

✅ Do

  • State the full 4 / 100 / 400 rule before coding
  • Keep parentheses around the compound predicate
  • Test 2024, 1900, and 2000
  • Reuse one helper for range scans
  • Clarify proleptic Gregorian scope if asked about history

❌ Don’t

  • Stop at year % 4 == 0
  • Reject all century years (misses 2000)
  • Ignore inverted range bounds
  • Mix astronomical year 0 with 1 BC casually
  • Claim the predicate is a full date library

Key Takeaways

Knowledge Unlocked

Five things to remember about leap years in C

Classify them the interview-friendly way.

5
Core concepts
% 02

Predicate

One boolean line

Code
C 03

Century

1900 no, 2000 yes

Edge
R 04

Range

Reuse the helper

Pattern
O 05

Complexity

O(1) per year

Analysis

❓ Frequently Asked Questions

A year is a leap year if it is divisible by 4, except years divisible by 100, unless they are also divisible by 400. Equivalently: leap iff (y%4==0 && y%100!=0) || (y%400==0).
Yes. 2000 is divisible by 400, so it is a leap year even though it is divisible by 100.
No. 1900 is divisible by 100 but not by 400, so it is a common year.
No. Adoption dates differ by country, and other calendars use different rules. This page uses the usual proleptic Gregorian arithmetic for integer years.
Historical numbering is messy; in code, test your product rule for negative y because C's % sign follows the dividend. This tutorial keeps examples to typical positive civil years.
A single test is O(1) arithmetic. Scanning a year range [a,b] costs O(b - a + 1) with O(1) extra space.
That is the Julian rule. Gregorian centuries that are not multiples of 400 are common years (1900, 2100), so the 100/400 exceptions matter.
366 days — February has 29 days instead of 28.

Did you Know? 🔊

The Julian calendar had a leap every four years without the century exception. The Gregorian reform (1582 in several countries) dropped 10 days and refined the rule so years like 1900 are common years while 2000 stays a leap year.

Continue to Magic Number

Learn how to check magic numbers with repeated digit-sum reduction in C.

Magic Number tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful