- Reason
- Divisible by
400
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
- Reason
- Century not divisible by
400
Live preview
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
function isLeapYear(y):
return (y mod 4 == 0 and y mod 100 != 0) or (y mod 400 == 0)Single year: 2024
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.");
}
}
}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.
Leap years in [2024, 2050]
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();
}
}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
🔄 Input / output examples
| Year | Leap? |
|---|---|
2024 | Yes |
2021 | No |
2000 | Yes |
1900 | No |
Edge cases and pitfalls
1900
Divisible by 100 but not 400, so not leap.
2000
Divisible by 400, so leap.
Invalid range
Handle startYear > endYear in range-based programs.
⏱️ Time and space complexity
| Task | Time | Extra space |
|---|---|---|
| One year check | O(1) | O(1) |
| Range scan | O(end - start + 1) | O(1) |
Summary
- Leap rule is based on
4 / 100 / 400divisibility. - One predicate function works for both single year and range checks.
- Remember classic exceptions:
1900no,2000yes.
Gregorian leap rule: divisible by 4, except centuries unless divisible by 400. That is why 1900 is not leap but 2000 is leap.
8 people found this page helpful
