Java Program to check Leap year or not

Here we are explaining how to write a java program to check whether a year is a leap year or not. So first we have to know what is a leap year and how we can check a year is a leap year or not.

What is a leap year?

A normal year has 365 days. Leap year is a year that has 366 days. Mostly which occurs every four years. If a year has February 29, then that year is a leap year.

How to check a year is a leap year or not?

  • If a year is a leap year, then that year can be exactly divided by 4 except the years ending with 00.

                 Example:2020

  • If a year is ending with 00, then we have to check it should be divisible by 400 also. If the year is divisible by 400, then it is a leap year.

                : Example:2000

                 

How to implement the program to check whether a year is a leap year or not?

So we have to declare the class LYear with a public modifier. Then read the year from the user and save it into the variable y. Then by using the if statement checks if y%4 equals zero or not. If zero then also check y0 equals zero and y@0 equals zero.If both of these conditions are true set flag=true.Else set flag=false.If y%4 equals zero and y % 100 not equals zero then we have to set flag=true else set flag=false. Then we have to check the flag value. If flag equals true then we will display the year as a leap year. If the flag equals false then we will display the year is not leap year.

 

ALGORITHM

STEP 1: Declare the class LYear with public modifier 

STEP 2: Open the main() to start the program, Java program execution starts with the main()

STEP 3: Declare the variable y as integer and flag as boolean. And initialize the value of the flag to false.

STEP 4: Read the year from the user into the variable y using a scanner.

STEP 5: check if y % 4 equals zero, if true then do step 6. Else do step 8.

STEP 6: Check if y 0 equals zero or not. If it's not zero then the set flag equals true.

STEP 7: If y 0 equals zero then check if y@0 equals zero, if true then set flag equals true, else set flag equals false. And go to step 9.

STEP 8: If y % 4 not equals zero then set  flag=false.

STEP 9: If flag equals true then display the year as a leap year. Else display the year is not leap year.

 

             

Java Source Code

                                          import java.util.Scanner;
public class LYear {

    public static void main(String[] args) {

        int y;
        boolean flag = false;
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the year : ");//Read the Year from the user.
        y = sc.nextInt();
        if(y % 4 == 0)
        {
            if( y % 100 == 0)
            {
                // if the year is divisible by 400 then it is leap year.
                if ( y % 400 == 0)
                    flag = true;
                else
                    flag = false;
            }
            else
                flag = true;
        }
        else
            flag = false;

        if(flag==true)
            System.out.println("It  is a leap year!");
        else
            System.out.println("It is not a leap year!");
    }
}
                                      

OUTPUT

Enter the year : 2000
2000 is a leap year!

Enter the year : 2001
2001 is not a leap year!