Rule
4 / 100 / 400
Divisible by 4, except most centuries, unless divisible by 400.
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.
4 / 100 / 400
Divisible by 4, except most centuries, unless divisible by 400.
is_leap_year
One boolean expression with %, &&, and ||.
1900 vs 2000
1900 is common; 2000 is leap because of the 400 rule.
2024–2050
List every leap year in a closed interval with the same helper.
Check year
Test any integer year with the same Gregorian predicate.
366 days
A leap year has 366 days; February gets an extra day.
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.
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.”
The full Gregorian predicate in one line.
A few modulo operations — constant time.
1900 fails; 2000 passes the 400 rule.
One function powers single checks and range scans.
In short: leap iff (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0).
Given an integer year, decide whether it is a Gregorian leap year; optionally list every leap year in a closed interval.
/* 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
*/ | Item | Type | Description |
|---|---|---|
year | int | Civil year to classify (Example 1 uses 2024). |
| Range bounds | int | Inclusive interval such as [2024, 2050] (Example 2). |
| Result | flag / text | Leap or not; or a printed list of leap years. |
function is_leap_year(y):
return (y mod 4 = 0 and y mod 100 != 0) or (y mod 400 = 0) | Task | Idea | Extra space |
|---|---|---|
| Single check | Gregorian boolean predicate | O(1) |
| Range scan | Call the same helper for each year in [L, R] | O(1) |
| Goal | Pattern |
|---|---|
| Leap predicate | (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0) |
| Quadrennial only | y % 4 == 0 — necessary but not sufficient |
| Century exception | y % 100 == 0 needs y % 400 == 0 to stay leap |
| Range loop | for (y = start; y <= end; ++y) |
| Classic probes | Test 2024, 1900, and 2000 |
Related ideas — interviews almost always want the Gregorian predicate.
4/100/400Standard civil calendar rule
y % 4Leap every four years — no century cut
mktimeProduction systems often delegate after validation
1900/2000Recite both century cases out loud
Reach for leap-year logic when calendar math or boolean predicates matter.
Quick check of %, short-circuit logic, and edge cases.
Decide February’s length when rendering a month grid.
Offsets after February depend on leap status.
List or count leap years in [L, R].
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.
Enter an integer year and see whether the Gregorian predicate says leap or common.
Two complete C programs — classify a single year, and list leap years in [2024, 2050]. Click View Output to reveal sample console results.
Compact predicate for year = 2024.
2024One boolean return; parentheses mirror the prose rule.
#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;
} Short-circuiting && and || match the usual truth table. For 2024, % 4 == 0 and % 100 != 0, so the function returns true.
Reuse the same helper across a closed year interval.
[2024, 2050]Scan each year independently; listing matches the classic reference output.
#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;
} Leap years are spaced by 4 until a century boundary; 2100 would break the pattern, but this interval stops at 2050.
Check year % 4 == 0. If false, the year is common.
If year % 100 == 0, the century exception may apply.
Centuries divisible by 400 remain leap years.
For 2024, % 4 passes and it is not a century — leap.
Trace the same predicate on a normal leap year, a failed century, and a successful 400-year.
| Year | % 4 | % 100 | % 400 | Result |
|---|---|---|---|---|
2024 | 0 | 24 | — | Leap |
1900 | 0 | 0 | 300 | Common |
2000 | 0 | 0 | 0 | Leap |
2021 | 1 | — | — | Common |
Takeaway: divisibility by 4 is necessary but not sufficient once centuries enter the picture.
Where leap-year thinking shows up beyond the interview prompt.
Combine modulo tests with short-circuit && / ||.
Example: one-line Gregorian predicate.
Choose 28 vs 29 days for February.
Example: month grid for March starts one day later in leap years.
Day-of-year and date diffs need leap awareness after Feb.
Example: ordinal day of March 1.
List or count leap years in an interval.
Example: all leaps in 2024–2050.
Reject Feb 29 on common years when parsing dates.
Example: input sanitizers for forms.
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.
Why this approach earns interview points.
One expression — easy to write, read, and explain.
A fixed number of arithmetic operations per year.
One is_leap_year powers single checks and range scans.
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.
Small habits that keep leap-year code clean in interviews.
State the rule out loud before typing the expression.
Make the && / || grouping obvious even if precedence would suffice.
Always dry-run 1900 and 2000 on paper.
If start > end, swap or reject before scanning.
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.
Mistakes that commonly break leap-year solutions in C.
That is the Julian rule; centuries like 1900 are not leap.
→ Include the 100 and 400 exceptions.
Rejecting all century years wrongly rejects 2000.
→ Centuries divisible by 400 remain leap.
Messy expressions without parentheses are hard to defend.
→ Write the grouped form that matches the spoken rule.
C’s % follows the dividend’s sign; historical numbering is messy.
→ Stick to positive civil years unless the prompt defines a policy.
Gregorian adoption dates differ by country.
→ Say you are using the proleptic Gregorian arithmetic rule.
Check these inputs before calling the solution done.
2024Divisible by 4, not a century — leap.
2000 and 2400 are leap; 2200 is not.
1900Divisible by 100 but not 400 — common.
Do not mix astronomical 0 with historical 1 BC without a clear convention.
start > endSwap bounds or guard the loop to avoid empty or inverted scans.
int widthTypical int is plenty for civil years.
Known results for common interview inputs.
| Year | Leap? |
|---|---|
2024 | Yes |
2021 | No |
2000 | Yes |
1900 | No |
Try these variations to lock in the pattern.
scanf and print leap / not leap[1900, 2000]?is_leap_year(y%4==0 && y%100!=0) || (y%400==0).1900 is common; 2000 is leap — memorize both.Quick Takeaway: leap iff divisible by 4, except centuries, unless divisible by 400 — then reuse that helper for range listings.
| Task | Time | Extra space |
|---|---|---|
| One year | O(1) | O(1) |
Range [a, b] | O(b - a + 1) | O(1) |
No auxiliary memory is required beyond loop indices.
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.
year % 4 == 0Classify them the interview-friendly way.
4 / 100 / 400
DefinitionOne boolean line
Code1900 no, 2000 yes
EdgeReuse the helper
PatternO(1) per year
AnalysisThe 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.
Learn how to check magic numbers with repeated digit-sum reduction in C.
8 people found this page helpful