In this article we will look into how to check if a year is leap year or not and also leap year program in Java. But first let’s look into definition of leap year.

## What is a Leap Year?

A **leap year** is a year which has 366 days. The extra day is the 29th February. There is a leap year every four years.

Every leap year has following properties :

- A century year i.e. year ending with 00 like 1900 is a leap year only if it is divisible by 400.
- A leap year (except a century year) can be identified if it is exactly divisible by 4.

*So therefore, a century year should be divisible by 4 and 100 both whereas non-century year should be divisible only by 4.*

Now let us see leap year program in java.

## Leap Year Program In Java

**Algorithm:**

`First, check if year is divisible by 4 or not i.e.`

*year % 4 == 0*`If no, return false as it is not a leap year.`

`If yes, we need to check if year is century year by checking remainder with 100 i.e`

*year % 100 == 0*`If it is a century year, check whether year is divisible by 400 or not.`

`If century year is divisible by 400, it is leap year else not.`

`If it is not century year, since we have already checked it is divisible by 4, hence it is leap year`

**Implementation**:

// Leap Year Program In Java import java.io.*; // Class for leap-year dealing public class LeapYear { // Method to check leap year public static boolean isLeapYear(int year) { // Check if year is divisble by 4 if (year % 4 == 0) { // Check if it is century year or not if (year % 100 == 0) { // Check leap year for century year by dividing by 400 if (year % 400 == 0) return true; else return false; } // If it is not century year, since it is divisible by 4, it is leap year return true; } // since year is not divisble by 4, it is not leap year. return false; } public static void main(String[] args) { int year = 2000; if (isLeapYear(year)) System.out.println(year + " : Leap-year"); else System.out.println(year + " : Not Leap-year"); } }

**Complexity:** O(1)

#### Alternative Way 1

// Leap Year Program In Java import java.io.*; // Class for leap-year dealing public class LeapYear { // Method to check leap year public static void isLeapYear(int year) { //Checking leap year for Century Year if (year % 400 == 0) { System.out.println(year + " is a leap year"); } // Check if year was century or not. else if (year % 100 == 0) { System.out.println(year + " is not a leap year"); } // Checking leap year for non century year else if (year % 4 == 0) { System.out.println(year + " is a leap year"); } else { System.out.println(year + " is not a leap year"); } } public static void main(String[] args) { isLeapYear(2000); isLeapYear(1900); } }

#### Alternative Way 2

In this approach we will condense the all the if else conditions above to single statement.

- The condensed formula to check leap year will be (
*year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))* - The condition checks leap for century year (
*year % 400 == 0)* - And the condition checks for non century year
*((year % 4 == 0) && (year % 100 != 0))* - If any of the above is true, it is leap year, else not

**Implementation**

// Leap Year Program In Java import java.io.*; // Class for leap-year dealing public class LeapYear { // Method to check leap year public static boolean isLeapYear(int year) { if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))) { // check for century and non century year return true; } // If above condition not true, it is non leap year return false; } public static void main(String[] args) { int year = 2000; if (isLeapYear(year)) System.out.println(year + " : Leap-year"); else System.out.println(year + " : Not Leap-year"); } }

## Conclusion

In this article we discussed various leap Year program in Java. So to conclude, a year if it is a century year should be divisible by 4 and 100 both whereas non-century year should be divisible only by 4 to be classified as leap year.

Got a question or just want to chat? Comment below or drop by our forums, where a bunch of the friendliest people you’ll ever run into will be happy to help you out!