Java Program to Check Leap Year

In this post we will go through basic java program to determine leap year.

Logic of Leap Year:

1. The year must be divisible by 400.
2. The year must be divisible by 4 but not 100.

For example :

  • 1998 is not a leap year.
  • 2000 is a leap year.
  • 2003 is not a leap year.

Algorithm to check a Year is Leap or not:

  •  Start the program.
  •  Read/Input the year.
  • Check if the year variable is divisible by 400.
  • Check if the year variable is divisible by 4 but not by 100.
  • If the above conditions are satisfied, print it’s a Leap year.
  • else not It’s not a Leap Year.

Approach 1 : Using if-else Statement 1

//leap year
public class Main{
   public static void main (String[]args)
   {

     int year = 2021;

     if (year % 400 == 0)
       System.out.println (year + " is a Leap Year");

     else if (year % 4 == 0 && year % 100 != 0)
       System.out.println (year + " is a Leap Year");

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

   }
 }

Output:

2021 is not a Leap Year

Approach 2 : Using if- else Statement 2:

public class Main
 {
   public static void main (String[]args)
   {

     int year = 2020;

     if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))
       System.out.println (year + " is a Leap Year");

     //not leap year
     else
         System.out.println (year + " is not a Leap Year");

   }
 }

Output:

2020 is a Leap Year

 

For more practice java programs:

Java Program to Check Number is Even or Odd

Java Program to Get Input from User

Addition of two numbers in java

Leave a Comment

%d bloggers like this: