Software & Finance





Java - Checking Leap Year





Here is the Java Source Code to check whether given year is a leap year or not.

 

First Rule: The year divisible by 4 is a leap year.

 

Second Rule: If the year is divisible by 100, then it is not a leap year. But If the year is divisible by 400, then it is a leap year.

 

 

Source Code


import java.io.*;

 

 

class LeapYear {

 

 

      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;

            }

            return false;

      }

 

 

 

    public static void main(String[] args) {

 

            String inpstring = "";

            InputStreamReader input = new InputStreamReader(System.in);

            BufferedReader reader = new BufferedReader(input);

 

            try

            {

                  System.out.print("Enter the Year to check Leap Year: ");

                  inpstring = reader.readLine();

 

                  int year = Integer.parseInt(inpstring);

                 

                 

                  if(IsLeapYear(year) == true)

                        System.out.println(year + " is a Leap Year");

                  else

                        System.out.println(year + " is NOT a Leap Year");

            }

            catch (Exception e)

            {

                  e.printStackTrace();

            }

    }

}

 

 

Output


C:\Java\Samples>javac LeapYear.java

 

C:\Java\Samples>java LeapYear

Enter the year to check leap year: 2000

Year 2000 is a leap year.

 

C:\Java\Samples>java LeapYear

Enter the year to check leap year: 1999

Year 1999 is NOT a leap year.