Beginners Programs :

Java Program To Check Leap Year Or Not – 4 Ways

Java Program to find if a year is a leap or not. Here, we see the various methods to find out whether a year is Leap or not in Java In 5 different ways. Soon Compiler is added so that you can execute the program yourself, along with suitable examples and sample outputs. The methods used in this case are:

    • Using Ternary Operator.
    • Using Static Method.
    • Using Command Line Arguments.
  • Using Function.
  • Using If-Else.

A LEAP YEAR, also known as an intercalary year or a bissextile year is a calendar year containing 366 days, as compared to the regular 365 days a year.

The extra day is added to balance out the drift between an astronomical year and a seasonal year. The Earth’s revolution generally takes 365.25 days approx to complete. The extra day is added every four years to bridge that extra time period. Here’s an interesting link that explains why years 1800, 1900, 2100 are not Leap years, even though they are divisible by 4.

Using Ternary Operator

1) The ternary operator syntax is  “if (condition)? value1:value2;”, if the condition is true then it returns value1, otherwise it returns value2.

2) We are calculating the given year is a leap year or not. A year divisible by 400 (or) a year divisible by 4 and not divisible by 100 is a leap year.

3) Read the year value using scanner object sc.nextLong and store it in the variable y.

In given example y=1948, y!=0, it is divisible by 400 so c=1 return to a, a is initialized as a=1. Then it prints 1948 is a leap year.

Output:

Using Static Method

1) In this program leapOrNot(long year) is the static method which finds the given year is a leap year or not. leapOrNot(long year) method calls at the main method of the class Leapyear. Then that static method starts the execution.

a) If the year divisible by 400 then it prints the given year is leap(or) If the given year divisible by 4 and not divisible by 100 then it prints the given year is a leap.

b) Otherwise, it prints “year is not a leap year”.

Output:

Using Command Line Arguments

1) We can read command line arguments from “String args[]” of the main method.

2) Convert the year which is in the string format at arg[0], in to int using year=Integer.parseInt(arg[0]);. Integer is the wrapper class which converts the string into int data type.

a) If the year divisible by 400 then it prints the given year is leap or if the given year divisible by 4 and not divisible by 100 then it prints the given year is a leap.

b) Otherwise, it prints “year is not a leap year”.

Output:

Java Leap Year Or Not – Using Function

1) The leap(int year) function returns c=1 if the given year is a)divisible by 400 or b) divisible by 4 and not divisible by 100.

2) The function calls at the main method if year!=0, then it returns the c value and c value initialized to the variable “a” if a=1 then prints  “is a leap year” otherwise prints “not a leap year”.

Output:

Using If-Else

1) We are using if else condition to find the given year is a leap year or not.

2) The 1st if condition checks the given year is equal to zero or not, if not

a) If given year divisible by 400 it prints  “is a leap year”, if condition false then come to else part.

b) If given year divisible by 100, then inner if condition checks the year is divisible by 4 or not if true then prints “is a leap year” otherwise prints “is not a leap year. If condition if(year%100!=0) is false then it prints “is not a leap year”.

Output:

More Java Programs: