Definition
Feb 29
Leap years insert an extra day under Gregorian rules.
A Gregorian leap year has February 29 under the 4 / 100 / 400 divisibility rules. This tutorial covers the predicate, century exceptions, a live preview, worked Python examples, edge cases, and complexity.
Feb 29
Leap years insert an extra day under Gregorian rules.
Predicate
(y%4==0 and y%100!=0) or (y%400==0).
Leap
Divisible by 4 and not a century year.
1900 vs 2000
1900 fails; 2000 passes (% 400).
Try any year
Instant Gregorian leap verdict in the browser.
Per year
A few modulus checks; range scan is linear.
A leap year in the Gregorian calendar has February 29. The arithmetic rule is: divisible by 4, except century years, which must also be divisible by 400.
Example: 2024 is leap (divisible by 4, not by 100). 1900 is not; 2000 is — because of the % 400 override.
Leap rules show up in date validation, scheduling, and interview prompts that test careful boolean logic with modulus.
A short boolean expression decides leap vs common.
% 100 alone would wrongly reject 2000.
One-liner boolean or nested ifs.
Code applies the rule uniformly to integer years.
In short: leap if divisible by 4 but not by 100, unless also divisible by 400.
Given an integer year y, decide whether it is a Gregorian leap year.
# 2024 → %4==0, %100!=0 → leap
# 1900 → %100==0, %400!=0 → not
# 2000 → %400==0 → leap
# 2021 → %4!=0 → not | Item | Type | Description |
|---|---|---|
year | int | Integer year (proleptic Gregorian formula). |
| Return / print | bool / text | True if the year is leap. |
function is_leap_year(y):
return (y mod 4 = 0 and y mod 100 != 0) or (y mod 400 = 0) | Method | Idea | Notes |
|---|---|---|
| Boolean one-liner | (%4 and not %100) or %400 | Interview favorite — compact |
| Nested ifs | Branch on %4, then centuries | Easier to explain step by step |
calendar.isleap | Standard library | Great in apps; write it yourself in interviews |
| Goal | Pattern |
|---|---|
| Leap predicate | (y % 4 == 0 and y % 100 != 0) or (y % 400 == 0) |
| Common year | y % 4 != 0 |
| Century leap | y % 400 == 0 |
| Yes classics | 2024, 2000, 2012 |
| No classics | 2021, 1900, 2100 |
| Range loop | for y in range(a, b + 1) |
Same Gregorian rule — pick the form that fits the interview.
one expressionCompact and easy to memorize
step by stepClearer for whiteboard walkthroughs
isleap(y)Prefer in production apps
cite 1900/2000Shows you know century exceptions
Reach for leap-year checks when calendar math and modulus logic appear.
Boolean logic with a classic real-world rule.
Decide whether February 29 is allowed.
Print leap years in a classroom interval.
Another short divisibility warm-up in this chain.
Leap predicate alone is not complete date parsing.
Key benefit: a tiny O(1) check that still forces careful handling of special cases (centuries).
Check any integer year in JavaScript safe range (|year| ≤ 1,000,000).
Three complete Python programs — single-year check for 2024, range 2024–2050, and a nested-if style. Click View Output to reveal sample console results.
One predicate matching the textbook Gregorian rule.
2024Checks one year with the standard boolean expression.
def is_leap_year(year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
year = 2024
if is_leap_year(year):
print(f"{year} is a leap year.")
else:
print(f"{year} is not a leap year.") 2024 is divisible by 4 and not by 100, so the first clause is true. The boolean expression matches the textbook leap rule directly.
Reuse the same helper to filter a year interval.
Prints all leap years in the beginner demo range.
def is_leap_year(year: int) -> bool:
return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)
start_year = 2024
end_year = 2050
print(f"Leap years in the range {start_year} to {end_year}:")
for y in range(start_year, end_year + 1):
if is_leap_year(y):
print(y, end=" ")
print() The pattern is mostly every 4 years. No century year falls inside this interval, so you only see the regular 4-year cadence.
Same rule, explained branch by branch — great for whiteboards.
Checks classics including century years 1900 and 2000.
def is_leap_year_nested(year: int) -> bool:
if year % 4 != 0:
return False
if year % 100 != 0:
return True
return year % 400 == 0
for value in (2024, 2021, 1900, 2000):
label = "leap" if is_leap_year_nested(value) else "common"
print(f"{value}: {label}") First reject years not divisible by 4. Non-century years that pass are leap; century years need the final % 400 test.
If year is not divisible by 4, it is common.
If divisible by 100, require divisible by 400.
True for leap, False for common.
Gregorian predicate complete.
Trace the three modulus checks on famous test years.
| Year | % 4 | % 100 | % 400 | Result |
|---|---|---|---|---|
2024 | 0 | 24 | — | Leap |
2021 | 1 | — | — | Common |
1900 | 0 | 0 | 300 | Common |
2000 | 0 | 0 | 0 | Leap |
Remember: century years need the % 400 override.
Where leap-year checks show up beyond the interview prompt.
Boolean logic with a memorable real-world rule.
Example: write is_leap_year(y).
Allow February 29 only in leap years.
Example: form input for birthdays.
List leap years in a classroom interval.
Example: 2024 to 2050 list above.
% 4 / % 100 / % 400 drills in one problem.
Example: compare 1900 and 2000.
366 vs 365 when summing days in a year.
Example: day-of-year helpers.
Explain why century exceptions exist.
Example: average year length ~365.2425.
Pro Tip: recite “divisible by 4, except centuries unless divisible by 400” before coding.
Why this pattern works well in interviews and classwork.
O(1) time with a few modulus operations.
2024 / 1900 / 2000 catch most bugs instantly.
One sentence rule maps cleanly to code.
Boolean one-liner or nested ifs — same result.
Pro Tip: lead with the one-liner; switch to nested ifs if the interviewer wants a verbal walkthrough.
Small habits that keep leap-year solutions interview-ready.
Say 4 / 100 / 400 before writing code.
Century pair catches the most common bug.
Group and / or so precedence is obvious.
Swap or reject when start > end.
Code applies Gregorian arithmetic uniformly.
Pro Tip: if you forget the century rule, you will call 1900 a leap year — always verify that case.
Mistakes that commonly break leap-year solutions.
Marks 1900 as leap incorrectly.
→ Always include the 100 / 400 century logic.
Rejecting every % 100 year also rejects 2000.
→ Allow centuries when % 400 == 0.
Missing parentheses around and / or.
→ Parenthesize the full predicate.
Assuming Julian rules or local historical quirks.
→ State that this page uses Gregorian arithmetic.
start > end silently prints nothing.
→ Validate or swap bounds first.
Leap predicate is only one part of full date validation.
2000 is leap; 1900 is not.
Code usually applies the Gregorian formula uniformly.
start > endGuard or swap bounds in range loops.
Reject non-integers in UI / preview parsing.
2100 is not leap (% 400 != 0).
Formula still runs; historical meaning differs.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
(y%4==0 and y%100!=0) or (y%400==0).Quick Takeaway: leap if divisible by 4 but not 100, unless also divisible by 400.
| Task | Time | Extra space |
|---|---|---|
| One year check | O(1) | O(1) |
| Range [a, b] | O(b - a + 1) | O(1) |
| Nested-if style | O(1) | O(1) |
No extra structures needed beyond simple counters.
Gregorian leap years follow a short modular rule: divisible by 4, except centuries unless divisible by 400. Encode that as a predicate, test 1900 and 2000, then reuse it for range scans.
Practice the three examples above, then continue to magic numbers for another digit-based warm-up.
2024 and 2000 are leap; 2021 and 1900 are not — memorize those four checks.
Decide leap vs common the interview-friendly way.
4 / 100 / 400
DefinitionThree checks
Code1900 no / 2000 yes
EdgeExtra day
CalendarO(1) per year
AnalysisGregorian reform refined the simple every-4-years rule: century years are not leap years unless divisible by 400.
Learn how magic numbers repeatedly sum digits until they reach 1.
8 people found this page helpful