Write a Java Program to Check Leap Year

As a Java programmer, one of the most common tasks that you may encounter is checking whether a year is a leap year or not.

A leap year is a year that is evenly divisible by 4, except for years that are divisible by 100 but not by 400.

In this tutorial, we will demonstrate how to write a Java program that checks if a given year is a leap year or not.


To check if a year is a leap year or not, we can follow the following steps:

  1. Get the year from the user as an input.
  2. Check if the year is divisible by 4 using the modulo operator (%).
  3. If the year is divisible by 4, check if it is divisible by 100.
  4. If the year is divisible by 100, check if it is also divisible by 400.
  5. If the year is divisible by 400, then it is a leap year. If not, it is not a leap year.

Here is the Java code that implements the above algorithm:

import java.util.Scanner;

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

        boolean isLeapYear = false;

        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) {
                    isLeapYear = true;
                }
            } else {
                isLeapYear = true;
            }
        }

        if (isLeapYear) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
    }
}

In the above code, we first ask the user to enter a year.

Then, we initialize a boolean variable isLeapYear to false. We then check if the year is divisible by 4 using the modulo operator.

If the year is divisible by 4, we then check if it is divisible by 100. If it is, we then check if it is also divisible by 400.

If it is divisible by 400, then we set the isLeapYear variable to true.

If it is not divisible by 400, then we leave the isLeapYear variable as false.

Finally, we print out whether the year is a leap year or not based on the value of the isLeapYear variable.


In conclusion, checking whether a year is a leap year or not is a simple task that can be easily accomplished in Java using the modulo operator and a few if statements.

By following the above algorithm and using the provided Java code, you can easily implement a leap year checker program in Java.