A leap year has 366 days (February has 29). In the Gregorian calendar, leap years follow the 4 / 100 / 400 rule.
Remember
year is leap ⇔
(year % 4 == 0 AND year % 100 != 0)
OR
(year % 400 == 0)
Examples:
2024 → divisible by 4, not by 100 → leap
1900 → divisible by 100, not by 400 → not leap
2000 → divisible by 400 → leap
2023 → not divisible by 4 → not leap
Year
Why
Leap?
2024
% 4 == 0, not a century
Yes
2000
% 400 == 0
Yes
1900
century, not % 400
No
2023
not % 4
No
Approach
How to Check a Leap Year in Java
Four clear steps — then write it as one expression or nested ifs.
If the year is not divisible by 4, it is not a leap year.
If it is divisible by 4 but not by 100, it is a leap year.
If it is divisible by 100, it must also be divisible by 400.
Otherwise it is not a leap year.
Method
Idea
Time
Best for
Boolean expression
One-line 4/100/400 predicate
O(1)
Interviews, short code
Nested if
Same rule, branched for reading aloud
O(1)
Explaining each condition
Pseudocode
Pseudocode
function IsLeapYear(year):
if year mod 400 == 0:
return true
if year mod 100 == 0:
return false
if year mod 4 == 0:
return true
return false
Reference
Cheat Sheet
Keep these facts handy while writing the program.
Topic
Detail
Rule
(%4 && !%100) || %400
February
29 days in a leap year
Century leap
Only if divisible by 400
Classic yes
2024, 2000
Classic no
1900, 2023
Built-in
Year.isLeap(year) (java.time)
Try it
Live Preview
Enter any year to see each divisibility check and the final verdict.
Whole years from 1 to 9999. Tap a chip or type a year.
Result
year = 2024
2024 % 4 = 0
2024 % 100 = 24 (not a century year)
Verdict: LEAP YEAR
Trace
Worked Walkthrough — year = 2000 and 1900
Century years look similar until you apply the % 400 test.
Calculation
year = 2000
2000 % 400 = 0 → leap ✓
year = 1900
1900 % 400 = 300 ≠ 0
1900 % 100 = 0 → century, not leap ✗
year = 2024
2024 % 4 = 0
2024 % 100 ≠ 0 → leap ✓
Code
Java Programs
Start with a one-line boolean expression. Use nested ifs when you want to explain each rule out loud.
Example 1 — Boolean Expression
Encode the Gregorian rule in a single return statement.
Java
public class LeapYear {
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
How It Works
year % 4 == 0 is the basic every-4-years test.
year % 100 != 0 excludes most century years.
year % 400 == 0 brings century leaps like 2000 back in.
The || joins both paths into one boolean result.
For 2024: divisible by 4, not by 100 → leap.
Example 2 — Nested If
Same logic, written as branches so each rule is easy to narrate in an interview.
Java
public class LeapYearNested {
static boolean isLeapYear(int year) {
if (year % 400 == 0) {
return true;
}
if (year % 100 == 0) {
return false;
}
if (year % 4 == 0) {
return true;
}
return false;
}
public static void main(String[] args) {
System.out.println(isLeapYear(2000)); // true
System.out.println(isLeapYear(1900)); // false
System.out.println(isLeapYear(2024)); // true
}
}
Output
true
false
true
How It Works
Check % 400 first — strongest leap rule for centuries.
Then reject remaining century years with % 100 == 0.
Then accept ordinary leap years with % 4 == 0.
Anything left returns false.
Same answers as Example 1 — pick the style that is clearer to explain.
Edge Cases & Pitfalls
Check these before calling the solution done.
Centuries
Not all % 4 years leap
1900 fails; 2000 passes because of % 400.
Only % 4
Incomplete rule
Interviewers often probe century years on purpose.
Order
Test 400 before 100
In nested form, checking % 100 first without % 400 breaks 2000.
Built-in
Year.isLeap
Fine in apps (java.time.Year); write the rule yourself in interviews.
Analysis
Time and Space Complexity
Program
Time
Extra space
Boolean expression
O(1)
O(1)
Nested if
O(1)
O(1)
A single-year check is a few modulus operations. Scanning a year range is O(range size).
Remember
Key Takeaways
Rule:(%4 && !%100) || %400.
Century trap:1900 no, 2000 yes.
Two styles: one expression or nested ifs.
Test set: 2024 yes; 2023 no; 2000 yes; 1900 no.
One line: leap if divisible by 4 but not 100, or divisible by 400.
Frequently Asked Questions
In the Gregorian calendar: leap if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0).
Yes. It is divisible by 400, so the century exception does not apply.
No. It is divisible by 100 but not by 400.
No. Century years need the extra % 400 test — that is why 1900 fails and 2000 passes.
Yes in real projects (java.time.Year). Interviews usually want you to write the 4/100/400 rule yourself.