Scanner class Methods in Java

Program - To Check if the year is Leap year or not?

 Write program to check whether the year is leap year or not?

In this post, we will write two methods to find the leap year. First using Calendar class and then using simple logic.

Method 1: Using Calendar class 

Java have a Calendar class with which we can perform operation with date and time-zones. In this method, we will create a Calendar object by assigning instance of Calendar class to a variable of Calendar type. Then using this variable we will set the year of Calendar to the user entered year.Then we can check the number of days in a year and test them using if condition.If the number of day present in the user entered year are greater than 365 then it is a leap year otherwise not a leap year.

Method 2: Basic Logic

The basic logic of finding a leap year is to use test conditions. If the year has modulus other than 0 on dividing with 400 or (||) 100 and (&&)  modulus (Remainder) is 0 on dividing with 4. That means year is a Leap Year. Otherwise, When division of year with 4 produces remainder other then 0  then year is not a leap year.

Lets, have a look at the program

Program to check whether its leap year or not

Output:


Program to check whether its leap year or not

    Try it Yourself! Happy Coding!

Comments