0
0
JavaProgramBeginner · 2 min read

Java Program to Check Leap Year with Output and Explanation

You can check a leap year in Java by using the condition if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) to print that the year is leap; otherwise, it is not leap.
📋

Examples

Input2020
Output2020 is a leap year
Input1900
Output1900 is not a leap year
Input2000
Output2000 is a leap year
🧠

How to Think About It

To check if a year is leap, first see if it is divisible by 4. If yes, then check if it is not divisible by 100 unless it is also divisible by 400. This means years divisible by 400 are leap, years divisible by 100 but not 400 are not leap, and years divisible by 4 but not 100 are leap.
📐

Algorithm

1
Get the input year from the user
2
Check if the year is divisible by 400; if yes, it is a leap year
3
Else, check if the year is divisible by 4 but not by 100; if yes, it is a leap year
4
Otherwise, it is not a leap year
5
Print the result
💻

Code

java
import java.util.Scanner;

public class LeapYearCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
            System.out.println(year + " is a leap year");
        } else {
            System.out.println(year + " is not a leap year");
        }
        scanner.close();
    }
}
Output
Enter a year: 2020 2020 is a leap year
🔍

Dry Run

Let's trace the year 1900 through the code

1

Input year

year = 1900

2

Check divisibility by 400

1900 % 400 = 300 (not zero), so false

3

Check divisibility by 4 and not by 100

1900 % 4 = 0 (true), 1900 % 100 = 0 (true), so condition false because divisible by 100

4

Result

Year is not a leap year

ConditionResult
year % 400 == 0false
year % 4 == 0 && year % 100 != 0false
Leap year?No
💡

Why This Works

Step 1: Divisible by 400 means leap year

If year % 400 == 0 is true, the year is a leap year because every 400 years the calendar corrects itself.

Step 2: Divisible by 4 but not by 100 means leap year

If the year is divisible by 4 but not by 100, it is a leap year because most leap years follow this rule.

Step 3: Other years are not leap years

If neither condition is true, the year is not a leap year.

🔄

Alternative Approaches

Using Java's built-in Year class
java
import java.time.Year;

public class LeapYearCheck {
    public static void main(String[] args) {
        int year = 2020;
        if (Year.isLeap(year)) {
            System.out.println(year + " is a leap year");
        } else {
            System.out.println(year + " is not a leap year");
        }
    }
}
This method uses Java 8+ built-in API for simplicity and readability but requires Java 8 or higher.
Using nested if-else statements
java
import java.util.Scanner;

public class LeapYearCheck {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();

        if (year % 400 == 0) {
            System.out.println(year + " is a leap year");
        } else if (year % 100 == 0) {
            System.out.println(year + " is not a leap year");
        } else if (year % 4 == 0) {
            System.out.println(year + " is a leap year");
        } else {
            System.out.println(year + " is not a leap year");
        }
        scanner.close();
    }
}
This approach is more verbose but clearly separates each condition.

Complexity: O(1) time, O(1) space

Time Complexity

The program performs a fixed number of arithmetic and logical operations regardless of input size, so it runs in constant time O(1).

Space Complexity

The program uses a fixed amount of memory for variables and does not allocate extra space based on input, so space complexity is O(1).

Which Approach is Fastest?

All approaches run in constant time, but using Java's built-in Year.isLeap() method is more readable and less error-prone.

ApproachTimeSpaceBest For
Manual condition checkO(1)O(1)Simple and clear logic
Java Year.isLeap() methodO(1)O(1)Readability and built-in reliability
Nested if-elseO(1)O(1)Explicit condition handling
💡
Remember to check the divisibility by 400 first because it overrides the 100 rule.
⚠️
Beginners often forget that years divisible by 100 are not leap years unless divisible by 400.