**Java Programs**

➤ Check Even Number

➤ Check Odd Number

➤ Java Even-Odd

➤ Greatest of 3 numbers

➤ Exponents in Java

➤ Java Leap Year Program

➤ Display Multiplication Table

➤ Reverse of a number

➤ Factors of a Number

➤ Java LCM of 2 Numbers

➤ Java HCF of 2 Numbers

➤ Quadratic Equation Program

➤ Square Root of Number

➤ Perfect Square Program

➤ Simple Calculator Program

➤ BMI Calculator Java

➤ Factorial of a Number

➤ Factorial Using Recursion

**#**Java Programs to Find Sum

**#**Java Conversion Programs

**#**Java Program on Series

**#**Java Pattern Programs

**#**Java Number Programs

**Java Array Programs**

**Java String Programs**

**Leap Year Program in Java** | In this post, we will write a Leap Year Program in Java. Based on the given input it will check whether the given year is a leap year or not.

A year is called a leap year if the year is divisible by four, except for the years which are divisible by 100 but not divisible by 400. Therefore, the year 2000 was a leap year, but the years 1700, 1800, and 1900 were not.

The complete list of leap years in the first half of the 21st century is 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036, 2040, 2044, and 2048.

**Leap Year Program in Java Using If-Else**

```
import java.util.Scanner;
public class LeapYear {
public static void main(String[] args) {
// declare variables
int year = 0;
boolean result = false;
// create Scanner class object to read input
Scanner scan = new Scanner(System.in);
// read input
System.out.print("Enter year:: ");
year = scan.nextInt();
// check year is leap year or not
if(( year%4==0) && ( (year%400==0) || (year%100!=0) ) )
result = true;
else
result = false;
// display result
if(result == true)
System.out.println(year+" is a leap year");
else System.out.println(year+" is not a leap year");
// close Scanner class object
scan.close();
}
}
```

Output for different test cases:-

Enter year:: 2000

2000 is a leap year

Enter year:: 1900

1900 is not a leap year

In this program for your better understanding purpose, we have written logic to check year is a leap year or not inside the main method, but we should not do that. We should write logic inside a user-defined method and call it from the main method. Instead of defining logic in the main method, let us define them in a separate method.

## Leap Year Program in Java Using If-Else

```
import java.util.Scanner;
public class LeapYear {
public static boolean isLeapYear(int year) {
if ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0))) {
return true;
} else {
return false;
}
}
public static void main(String[] args) {
int year = 0;
boolean result = false;
Scanner scan = new Scanner(System.in);
System.out.print("Enter year:: ");
year = scan.nextInt();
result = isLeapYear(year);
if (result) {
System.out.println(year + " is a leap year");
} else {
System.out.println(year + " is not a leap year");
}
scan.close();
}
}
```

In this program, all conditions are written within the if condition, so it may look likes complicated to you. In the below program, the nested-if statement is used to solve the same problem. Now, you may understand the logic simply. The isLeapYear() method of this program also can be written using nested if-else.

## Leap Year Program in Java Using Nested If-Else

The same isLeapYear() method could be written using nested if-else as follows:-

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

## Leap Year Program in Java Using Ternary Operator

```
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0))) ? true : false;
}
```

The default value of the boolean data type is false therefore it is optional to return false in our case. The logic of the isLeapYear() method also can be written in one line as:-

```
public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)));
}
```

## Leap Year Program in Java Using Year Class

Java language also contains a predefined isLeap() method in the Year class to check whether the given year is a leap year or not. Program to check leap year using Year class:-

```
import java.time.Year;
import java.util.Scanner;
public class Test {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.print("Enter year: ");
int num = scan.nextInt();
Year year = Year.of(num);
if (year.isLeap()) {
System.out.println(year + " is Leap Year.");
} else {
System.out.println(year + " is not a Leap Year.");
}
scan.close();
}
}
```

Output:-

Enter year: 2030

2030 Year is not a Leap Year.

Enter year: 2000

2000 is Leap Year.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!