In this article we will show you, How to write Java Leap Year Program using If Statement, Nested If Statement and Else If Statement in Java Programming with example. Before we get into program, Let us see the logic and definition behind the Leap Year

### Leap year

The normal year contains 365 days but leap year contains 366 days. This extra day is added to the February month, that’s why we get February 29. Mathematically, Years that are perfectly divisible by 4 are called as Leap years except the century years. Century year’s means they end with 00 such as 1200, 1300, 2400, 2500 etc (Obviously they are divisible by 100). For these century years we have to calculate further to check the Leap year.

- If the century year is divisible by 400 then that year is a Leap year
- If the century year is not divisible by 400 then that year is a Leap year

## Java Leap Year Program using If Statement

This Java program allows the user to enter any year and then it will check whether the user entered year is Leap year or not using the Java If Else statement

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Java Leap Year Program using If Statement package DatePrograms; import java.util.Scanner; public class LeapYearUsingIf { private static Scanner sc; public static void main(String[] args) { int year; sc = new Scanner(System.in); System.out.println("\n Please Enter any year you wish: "); year = sc.nextInt(); if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0))) { System.out.format("\n %d is a Leap Year. \n", year); } else { System.out.format("\n %d is NOT a Leap Year. \n", year); } } } |

**OUTPUT**

Let us check for normal year (Not Leap Year)

**ANALYSIS**

Since we have to check multiple conditions within one If Statement, we used Logical AND and Logical OR operators. Let us divide the condition to understand it better

1: ( year % 400 == 0)||

2: ( year % 4 == 0 ) &&

3: ( year % 100 != 0))

First condition (year%400 == 0) will check whether the (year%400) reminder is exactly equal to 0 or not. As per the algorithm any number that is divisible by 400 is a Leap year.

OR

Second condition holds 2 statements so they both has to be TRUE.

- First condition (year%4 == 0) will check whether the reminder of the (year % 4) is exactly equal to 0 or not. If the condition is False then it will exit from the condition because there is no point in checking the other condition. It is definitely not the Leap year.
**AND** - Second condition will check (year % 100) reminder is not equal to 0. If it is TRUE then the given number is not century number.
- As per the algorithm any number that is divisible by 4 but not divisible by 100 then that number is Leap Year.

**TIP: **Please refer Java Logical Operators to understand the functionality of Logical And and Logical Or.

## Java Leap Year Program using Else If Statement

This Java program allows the user to enter any year and then it will check whether the user entered year is Leap year or not using the Java Else If statement.

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
// Java Leap Year Program using Else If Statement package DatePrograms; import java.util.Scanner; public class LeapYearUsingElseIf { private static Scanner sc; public static void main(String[] args) { int year; sc = new Scanner(System.in); System.out.println("\n Please Enter any year you wish: "); year = sc.nextInt(); if ( year % 400 == 0) { System.out.format("\n %d is a Leap Year. \n", year); } else if (year%100 == 0) { System.out.format("\n %d is NOT a Leap Year. \n", year); } else if(year%4 == 0) { System.out.format("\n %d is a Leap Year. \n", year); } else { System.out.format("\n %d is NOT a Leap Year. \n", year); } } } |

**OUTPUT**

**ANALYSIS**

Following statements will ask the user to enter any year to check whether that year is Leap year or Not and we are going to assign the user entered value to year variable

1 2 |
System.out.println("\n Please Enter any year you wish: "); year = sc.nextInt(); |

- First If condition will check whether the (year % 400) reminder is exactly equal to 0. As per the algorithm any number that is divisible by 400 is a Leap year. If this condition Fails then it will go to next condition.
- Second If condition will check (year % 100) reminder is exactly equal to 0 or not. As per the algorithm any number that is not divisible by 400 but divisible by 100 is Not a Leap year (Century Year). We checked (year % 400) in the First If statement. Because it is failed it came to second condition. If both the first and second condition Fails then it will go to third condition.
- Third condition will check whether year mod 4 is equal to 0. If this condition is True then given year is Leap year because We already checked for the century years in the previous condition. If all the statements Fails then it will go Else statement at the end.
- If all the above statements fail then it is not Leap year

## Java Leap Year Program using Nested If Statement

This Java program allows the user to enter any year and then it will check whether the user entered year is Leap year or not using the Nested If in Java

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
// Java Leap Year Program using Nested If Statement package DatePrograms; import java.util.Scanner; public class LeapYearUsingNestedIf { private static Scanner sc; public static void main(String[] args) { int year; sc = new Scanner(System.in); System.out.println("\n Please Enter any year you wish: "); year = sc.nextInt(); if ( year % 4 == 0) { if (year%100 == 0) { if(year%400 == 0) { System.out.format("\n %d is a Leap Year. \n", year); } else { System.out.format("\n %d is NOT a Leap Year. \n", year); } } else { System.out.format("\n %d is a Leap Year. \n", year); } } else { System.out.format("\n %d is NOT a Leap Year. \n", year); } } } |

**OUTPUT**

**ANALYSIS**

User will enter any year to check whether that year is Leap year or Not. First If condition will check whether the reminder of the (year%4) is exactly equal to 0 or not.

- If the condition is False then the given number is definitely not the Leap year.
- If the condition is True then we have check further for the century year. So the compiler will go to the Nested If condition.

Second If condition will check (year%100) reminder is exactly equal to 0 or Not.

- If this condition is False then the year is not century year. So the given number is definitely Leap year.
- If the condition is True then we have check whether the number is divisible by 400 or not. So the compiler will goto to another Nested If condition.

In this condition, compiler will check whether the remainder of the (year%400) is exactly equal to 0 or not.

- If the condition is False then the given number is definitely not the Leap year.
- If the condition is True then the given number is Leap Year

## Java Leap Year Program using Oops

This Java program allows the user to enter any positive integer (year) and then it check whether the given year is a leap year or not. In this example, we are dividing the code using the Object Oriented Programming. To do this, First we will create a class which holds a method to reverse an integer recursively.

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 |
package DatePrograms; public class LeapYear { public int CheckLeapYear(int year) { if (( year%400 == 0)|| (( year%4 == 0 ) && ( year%100 != 0))) { return year; } else { return 0; } } } |

Within the Main program, we will create an instance of the above specified class and call the methods

**JAVA CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package DatePrograms; import java.util.Scanner; public class LeapYearUsingClass { private static Scanner sc; public static void main(String[] args) { int year, leap; sc = new Scanner(System.in); System.out.println(" Please Enter any year you wish: "); year = sc.nextInt(); LeapYear ly = new LeapYear(); leap = ly.CheckLeapYear(year); if(leap != 0) { System.out.format("\n %d is a Leap Year. \n", year); } else { System.out.format("\n %d is NOT a Leap Year. \n", year); } } } |

**OUTPUT**

**ANALYSIS**

**LeapYear Class Analysis:**

First we declared an integer function **CheckLeapYear ** with one argument. Within the function, we used the If statement to check whether the given year is leap year or Not and if it is True then it will return year otherwise, it return zero. We already explained the Logic in above example.

**TIP:** If you declare a method with void keyword then we can’t return any value. If you want to return any value then replace void with data type and add return keyword.

**Main Class Analysis:**

First we created an instance / created an Object of the **LeapYear** Class

1 |
LeapYear ly = new LeapYear(); |

Next, we are calling the **CheckLeapYear** method.

1 |
leap = ly.CheckLeapYear(year); |

Lastly, **System.out.println** statement will print the output.

Thank You for Visiting Our Blog

## Leave a Reply