Last Updated: June 7, 2026
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.
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.
year is divisible by 4. If not, it is not a leap year.Input:
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.