Leap Year Program in Java

In this article, we will implement a leap year program in Java.

Leap Year Program in Java

What is a Leap Year?

A leap year is a year that has an extra day added to it, making it 366 days long instead of the usual 365 days.

The concept of a leap year is necessary because it takes the Earth approximately 365.25 days to complete one orbit around the sun. To account for this extra fractional day, an extra day is added to the calendar every four years.

A year is considered a leap year if it meets the following conditions:

  1. The year is evenly divisible by 4.
  2. If the year is evenly divisible by 100, it must also be evenly divisible by 400 to be a leap year.

Java Code

Let’s develop the leap year program in Java using the above leap year logic.

public class LeapYearProgram {
    public static void main(String[] args) {
        int[] years = { 2020, 1900, 2000, 2022, 1984 };

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

    public static boolean isLeapYear(int year) {
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                return year % 400 == 0;
            } else {
                return true;
            }
        } else {
            return false;
        }
    }
}

Output

2020 is a leap year.
1900 is not a leap year.
2000 is a leap year.
2022 is not a leap year.
1984 is a leap year.

Conclusion: Leap Year Program in Java

In this article, we developed a simple leap year program in Java.

Please go through the curated list of programs: 15 Basic Java Programs: Building Foundation for improving your coding skills.

Leave a Reply

Your email address will not be published. Required fields are marked *