AlgoMaster Logo

Leap Year Check

Last Updated: June 7, 2026

easy
3 min read

Understanding the Problem

The calendar year is about 365.24 days long, so a plain 365-day year drifts out of sync with the seasons. Adding one extra day every four years corrects most of that drift, the familiar "divisible by 4" rule. But that slightly overshoots, so the rule skips the leap day on century years. That overcorrects, so one more refinement keeps the leap day on century years also divisible by 400.

Those rules stack into three nested conditions. The challenge is combining them into one clean test instead of a tangle of branches. The century exception and the 400 override can be expressed together as a single grouped condition.

Key Constraints:

  • Divisibility by 4 is the base rule → A year that is not divisible by 4 can never be a leap year, so this is the first thing to rule out.
  • Century years are the exception → A year divisible by 100 breaks the simple rule and is not a leap year on its own.
  • Divisibility by 400 overrides the exception → A year divisible by 400 is always a leap year, even though it is also divisible by 100.

Approach 1: Compound Condition

Intuition

Capture the whole rule in one boolean expression rather than three separate if statements. A leap year must be divisible by 4, and on top of that it must either escape the century rule or satisfy the 400 override.

That phrasing maps directly to code. "Divisible by 4" is year % 4 == 0. "Escapes the century rule" is year % 100 != 0. "Satisfies the 400 override" is year % 400 == 0. The last two are alternatives, so they join with OR, and the result combines with the divisible-by-4 requirement using AND:

(year % 4 == 0) && (year % 100 != 0 || year % 400 == 0)

The parentheses matter. The OR group has to be evaluated as a unit before it is combined with the divisible-by-4 check, otherwise the precedence would change the meaning.

Algorithm

  1. Check whether year is divisible by 4. If not, it is not a leap year.
  2. If it is divisible by 4, check the century exception: is it divisible by 100 but not by 400?
  3. A year divisible by 100 but not by 400 is not a leap year. Every other multiple of 4 is.
  4. Return the combined result.

Example Walkthrough

Input:

1900
year

Start with year % 4. Since 1900 / 4 = 475 exactly, the remainder is 0, so the first condition passes. Now evaluate the grouped part. Is 1900 % 100 != 0? No, 1900 is divisible by 100, so this is false. Is 1900 % 400 == 0? Dividing gives 4.75, so the remainder is not 0, and this is also false. The OR of two false values is false, so the full expression is true && false, which is false. The year 1900 is not a leap year.

false
output

Code