Check Leap Year in Java

What you’ll learn

  • Gregorian leap-year rule with 4/100/400 logic.
  • Single year check and range listing in Java.
  • Handling classic edge years like 1900 and 2000.

Prerequisites

Modulo operator, boolean expressions, and loops in Java.

  • Understand % checks divisibility.
  • Know how to combine conditions with && and ||.

Math definition

A Gregorian year y is leap if (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0).

Intuition

2000Leap
Reason
Divisible by 400
1900Not leap
Reason
Century not divisible by 400

Live preview

Live result
Press "Check leap".

Algorithm

Goal: evaluate the Gregorian leap predicate for a year.

Check divisibility by 4

If the year is not divisible by 4, it is a common year.

Apply century exception

Years divisible by 100 are leap only when also divisible by 400.

📜 Pseudocode

Pseudocode
function isLeapYear(y):
    return (y mod 4 == 0 and y mod 100 != 0) or (y mod 400 == 0)
1

Single year: 2024

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

    public static void main(String[] args) {
        int year = 2024;
        if (isLeapYear(year)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}
📤 Output
2024 is a leap year.

Explanation

The predicate checks the full Gregorian rule in one boolean expression: divisible by 4 and not by 100, or divisible by 400. For 2024, the first part is true, so the year is leap.

2

Leap years in [2024, 2050]

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

    public static void main(String[] args) {
        int startYear = 2024;
        int endYear = 2050;
        System.out.println("Leap years in the range " + startYear + " to " + endYear + ":");
        for (int y = startYear; y <= endYear; y++) {
            if (isLeapYear(y)) System.out.print(y + " ");
        }
        System.out.println();
    }
}
📤 Output
Leap years in the range 2024 to 2050:
2024 2028 2032 2036 2040 2044 2048

Explanation

The loop evaluates each year with the same isLeapYear helper and prints only matching years. In this interval, leap years appear every 4 years and there is no century exception boundary.

Optimization

Single check: direct condition is already O(1); no extra optimization needed.

Range listing: for long ranges, avoid string concatenation in loops; use buffered output.

❓ FAQ

Leap iff (y%4==0 && y%100!=0) || (y%400==0).
Yes, because it is divisible by 400.
No, because it is divisible by 100 but not by 400.
No. This page uses standard proleptic Gregorian arithmetic.
Java arithmetic still works, but civil-calendar interpretation depends on convention.
Single check is O(1); scanning a range is O(n) in range length.

🔄 Input / output examples

YearLeap?
2024Yes
2021No
2000Yes
1900No

Edge cases and pitfalls

Century

1900

Divisible by 100 but not 400, so not leap.

400 Rule

2000

Divisible by 400, so leap.

Bounds

Invalid range

Handle startYear > endYear in range-based programs.

⏱️ Time and space complexity

TaskTimeExtra space
One year checkO(1)O(1)
Range scanO(end - start + 1)O(1)

Summary

  • Leap rule is based on 4 / 100 / 400 divisibility.
  • One predicate function works for both single year and range checks.
  • Remember classic exceptions: 1900 no, 2000 yes.
Did you know?

Gregorian leap rule: divisible by 4, except centuries unless divisible by 400. That is why 1900 is not leap but 2000 is leap.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful