basics · beginner · ~15 min
Compose boolean operators correctly.
Decide whether a year is a leap year in the Gregorian calendar.
Implement int is_leap(int year) that returns 1 if year is a leap year and 0 otherwise. A year is a leap year when it is divisible by 4 and (not divisible by 100 or divisible by 400).
A single int year (e.g. 1900, 2000, 2024).
Returns 1 for a leap year, 0 otherwise.
is_leap(2000) -> 1 (divisible by 400)
is_leap(1900) -> 0 (divisible by 100 but not 400)
is_leap(2024) -> 1
is_leap(2023) -> 0
The Gregorian leap-year rule is a great example of compound boolean logic and operator precedence — exactly the kind of place a misplaced || produces a bug that bites every four centuries.
A single int year.
1 if year is a leap year, otherwise 0.
Use the full Gregorian rule, including the divisible-by-400 exception.
int is_leap(int year) { /* TODO */ return 0; }
Forgetting the 400-year exception. Mixing up && and ||. Missing parentheses around the y%100 / y%400 sub-expression.
Year 2000 (leap). 1900 (not leap). 2024 (leap). 2100 (not leap).
O(1).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.