basics · beginner · ~15 min
Compose boolean operators correctly.
Implement int is_leap(int year) returning 1 if the year is a Gregorian leap year, 0 otherwise. Divisible by 4 and (not by 100 or divisible by 400).
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.
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.