Skip to content

Leap Year



The Problem

Definition

A year normally has 365 days. A leap year has 366 days, with one extra day added to February.

Given a year, we need to determine:

Is this year a leap year?

At first, this looks like a simple divisibility problem.

It is not quite that simple.

There are rules and exceptions, which makes this a good example of how to build conditional logic.


Why Does a Leap Year Exist?

Analogy

A calendar year is not exactly the same length as Earth's trip around the Sun. The difference is roughly a quarter of a day each year.

If we ignored that difference, the calendar would slowly drift away from the seasons.

Adding one extra day every few years keeps the calendar aligned more closely with Earth's orbit.

The Gregorian calendar uses a set of rules to decide when that extra day should be added.


The Three Rules

A year is a leap year when:

  1. It is divisible by 400.
  2. Or, it is divisible by 4 but not by 100.

Another way to understand the rules is as a pipeline every year has to pass through:

This gives us a very important lesson:

When a general rule has exceptions, identify the exceptions before writing the final condition.


Turn the Rules Into Logic

Let's translate each rule into something the program can check.

Rule 1: Divisible by 4

We can use the modulo operator: year % 4 == 0

If the remainder is 0, the year is divisible by 4.

For example: 2024 % 4 = 0

So 2024 passes the first rule.


Rule 2: Century Years Are Different

A year divisible by 100 is a century year.

For example: 1900 % 100 = 0

Century years are not leap years by default.

So we need: year % 100 != 0

This means:

The year must not be divisible by 100.


Rule 3: The Exception to the Exception

There is one more rule.

A century year is a leap year if it is also divisible by 400.

For example: 2000 % 400 = 0

So 2000 is a leap year.

This gives us the complete logic:

Code
(year divisible by 4 AND not divisible by 100)OR(year divisible by 400)

Test the Logic Before Writing Code

Before implementing a condition, test it with a few carefully chosen values.

Year÷ 4÷ 100÷ 400Result
2023No——Not leap
2024YesNo—Leap
1900YesYesNoNot leap
2000YesYesYesLeap
Four years, chosen because each one takes a different path through the rules.

These four cases are enough to test the important branches of the problem.

Notice how 1900 and 2000 are especially important.

If your program only checks year % 4 == 0, both years would incorrectly be considered leap years.


Implementing the Solution

Now that the logic is clear, the code becomes straightforward.

Code
public static boolean isLeapYear(int year) {    return (year % 4 == 0 && year % 100 != 0)        || year % 400 == 0;}

Read the condition from left to right. The first half:

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

means:

The year is divisible by 4 and is not a century year.

Then:

|| year % 400 == 0

means:

Or it is divisible by 400.

That exactly matches the three calendar rules.


A Clearer if-else Solution

When you are learning conditional logic, an explicit version can sometimes be easier to understand:

Code
public static boolean isLeapYear(int year) {    if (year % 400 == 0) {        return true;    }     if (year % 100 == 0) {        return false;    }     return year % 4 == 0;}

This version follows the decision process directly:

Code
Divisible by 400?      ↓    Yes → Leap       No      ↓Divisible by 100?      ↓    Yes → Not leap       No      ↓Divisible by 4?      ↓    Yes → Leap    No  → Not leap

For beginners, this version is often easier to reason about.


Dry Run: 2024

Let's execute the logic step by step, with year = 2024.

Step 1

2024 % 400 == 0 — False.

Step 2

2024 % 100 == 0 — False.

Step 3

2024 % 4 == 0 — True.

Therefore: 2024 is a leap year.


Dry Run: 1900

Now consider a tricky case, with year = 1900.

Step 1

1900 % 400 == 0 — False.

Step 2

1900 % 100 == 0 — True.

Therefore: 1900 is not a leap year.

This is why checking only divisibility by 4 is not enough.


The Problem-Solving Lesson

Leap Year is useful beyond calendars.

It teaches a common pattern in programming:

Rule → Exception → Exception to the exception

When a problem gives you several conditions:

  1. Write down the rules in plain English.
  2. Identify exceptions.
  3. Check whether an exception has another exception.
  4. Convert each rule into a simple condition.
  5. Combine the conditions using && and ||.
  6. Test normal cases and edge cases.

Do not start by trying to write one complicated expression.

Understand the rules first. Then write the code.

This approach will help you solve many conditional problems more confidently, including the next one: Prime Numbers.


Common Mistakes

Watch out

Checking only divisibility by 4

year % 4 == 0 on its own incorrectly treats 1900 as a leap year.

Forgetting the 400 rule

2000 is divisible by both 100 and 400, so it is a leap year.

Using || where && is required

The year must be divisible by 4 and not divisible by 100.

Writing the condition before understanding the rules

Complex boolean expressions are much easier to write correctly after the rules have been converted into simple statements.


Quick Check

  1. Which of these is a leap year: 1900, 2023, 2024, 2100?
  2. Will the year 2100 be a leap year?
  3. Why is year % 4 == 0 on its own not enough?
  4. Why does the if-else version check 400 before it checks 100?
Show answer
  1. 2024. It is divisible by 4 and not divisible by 100, so it is a leap year. The other three do not satisfy the complete rules.
  2. No. 2100 is divisible by 4, but it is a century year and is not divisible by 400, so it is a common 365-day year.
  3. Because it ignores both exceptions. It would wrongly call every century year a leap year, including 1900, 2100 and 2200.
  4. Because each return exits the function immediately. Checking 400 first means a year like 2000 is answered before the 100 check below it can reject it. Reverse the two and every 400-year correction is lost.

Quick Recap

  • A leap year contains 366 days.
  • The Gregorian calendar uses divisibility rules to determine leap years.
  • Divisible by 4 → usually a leap year.
  • Divisible by 100 → not a leap year.
  • Divisible by 400 → leap year again.
  • The complete condition is (year % 4 == 0 && year % 100 != 0) || year % 400 == 0.
  • The important problem-solving pattern is general rule → exception → exception to the exception.
  • Always test normal cases and edge cases before trusting your condition.

Key takeaway

Do not memorize the leap-year formula. Learn to turn rules and exceptions into simple conditions, test the edge cases, and then combine them into code.