basics · beginner · ~15 min

Leap year check

Compose boolean operators correctly.

Challenge

Decide whether a year is a leap year in the Gregorian calendar.

Task

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

Input

A single int year (e.g. 1900, 2000, 2024).

Output

Returns 1 for a leap year, 0 otherwise.

Example

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

Edge cases

  • Century years (1900, 2100) are leap years only if 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.

Input format

A single int year.

Output format

1 if year is a leap year, otherwise 0.

Constraints

Use the full Gregorian rule, including the divisible-by-400 exception.

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.