basics · beginner · ~15 min

Leap year check

Compose boolean operators correctly.

Challenge

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).

Why this matters

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.

Starter code

int is_leap(int year) { /* TODO */ return 0; }

Common mistakes

Forgetting the 400-year exception. Mixing up && and ||. Missing parentheses around the y%100 / y%400 sub-expression.

Edge cases to handle

Year 2000 (leap). 1900 (not leap). 2024 (leap). 2100 (not leap).

Complexity

O(1).

Background lessons

Up next

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.