Leap Year Java Program

Hi guys!! Welcome to flower brackets blog. Today you will learn leap year java program with an example.

leap year java program

As everybody know leap year occurs every four years as opposed to common year. Now we will see leap year java program,

Also Read – Date Time Java Program

Example : leap year java program without scanner class

public class LeapYear
{
   public static void main(String[] args)
   {
      int year = 2007;

      if((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0))
      {
         System.out.println("Year " + year + " is a leap year.");
      }
      else
      {
         System.out.println("Year " + year + " is not a leap year.");
      }
   }
}

Output: Year 2000 is a leap year.


Now with Scanner class let’s see isleapyear java,

Example : leap year java program with scanner class

Also Read – Date Time Java Program

import java.util.Scanner;

public class LeapYear
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter year: ");
      int year = sc.nextInt();

      if((year % 400 == 0) || (year % 4 == 0) && (year % 100 != 0))
      {
         System.out.println("Year " + year + " is a leap year.");
      }
      else
      {
         System.out.println("Year " + year + " is not a leap year.");
      }
   }
}

Output:

Please enter year: 2004
Year 2004 is a leap year.


Using if else loop

Now we will see leap year program java using if else loop. Let’s see leap year java code,

import java.util.Scanner;

public class LeapYearDemo
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter year : ");
      int year = sc.nextInt();
      boolean temp = false;
      if(year % 400 == 0)
      {
         temp = true;
      }
      else if (year % 100 == 0)
      {
         temp = false;
      }
      else if(year % 4 == 0)
      {
         temp = true;
      }
      else
      {
         temp = false;
      }
      if(temp)
      {
         System.out.println("The year " + year + " is a Leap Year");
      }
      else
      {
         System.out.println("The year " + year + " is not a Leap Year.");
      }
      sc.close();
   }
}

Output:

Please enter year : 2000
The year 2000 is a Leap Year


Using method

Now we will how to calculate leap year in java using method. Let see leap year algorithm java,

public class LeapYearExample
{
   public static void main(String[] args)
   {
      LeapYearExample leap = new LeapYearExample();
      System.out.println("1993 is a leap year : " + leap.findLeapYear(1993));
      System.out.println("2000 is a leap year : " + leap.findLeapYear(2000));
      System.out.println("2004 is a leap year : " + leap.findLeapYear(2004));
   }
 
   public boolean findLeapYear(int year)
   {
      if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
      {
         return true;
      }
      else
      {
         return false;
      }
   }
}

Output:

1993 is a leap year : false
2000 is a leap year : true
2004 is a leap year : true


conclusion

That’s it guys. This is all about java leap year program. I hope you have understood the topic.

You can subscribe to my blog flower brackets if you haven’t already. Do share this post if you like.

Related Posts