LeapYear In Java With Example Program

Hey there! I’m sure you’ve all heard of the term “Leap Year,” right? A leapyear is a year that has 366 days instead of the usual 365. This happens every 4 years, and an extra day is added to the month of February. In this article, I’m going to show you how to write a program in Java that can calculate whether or not a given year is a leap year. Stay tuned!

Post Type:Java Programs For Beginners
Published On:www.softwaretestingo.com
Applicable For:Freshers & Experience
Get Updates:Join Our Telegram Group

What is a Leap Year?

A leapyear has 366 days. To check if a year is a leap year, there are certain conditions that must be met:

  • The respected year should be divisible by 4.
  • If a year is divisible by 100 and also 400 then that year is a leapyear.

A leapyear is a year that is divisible by 4 with the exception of century years. A century year is only a leap year if it’s perfectly divisible by 400.

What Is the History of Leap Year?

The calendar you use today, the Gregorian calendar, was first introduced in 1582. It was a modification of the earlier Julian calendar and it solved the problem of the slight miscalculation in the length of a year by omitting three leapyears every four hundred years. TheGregoriancalendar is named after Pope Gregory XIII who introduced this system.

When Is the Next Leap Year?

Leap YearLeap Day
2024Thursday, February 29
2028Tuesday, February 29
2032Sunday, February 29
2036Friday, February 29

We can find out whether a year is a Leapyear or not by following the below methods:

  • Using if-else Statements
  • Using Ternary Operator
  • Boolean method

Leap Year Program Using If-else Statements – 1

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class LeapYearEx1
{
	public static void main(String[] args) 
	{
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter a Valid Year: ");
		int year=sc.nextInt();

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

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

		else
			System.out.println (year + " is not a LeapYear");
	}
}

Output:

Enter a Valid Year: 2000
2000 is a LeapYear

LeapYear Program Using If-else Statements – 1

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class LeapYearEx2
{
	public static void main(String[] args) 
	{
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter a Valid Year: ");
		int year=sc.nextInt();

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

		//not leapyear
		else
			System.out.println (year + " is not a LeapYear");
	}
}

LeapYear Program Using Ternary Operator

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class LeapYearEx3
{
	public static void main(String[] args) 
	{
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter a Valid Year: ");
		int year=sc.nextInt();

		int flag = (year%400 == 0) || (year%4==0 && year%100!=0 ) ? 1 : 0;
		if (flag ==1)
		{
			System.out.println (year + " is a LeapYear");
		}
		else
		{
			System.out.println (year + " is not a LeapYear");
		}
	}
}

Output:

Enter a Valid Year: 2028
2028 is a LeapYear

LeapYear Program Using Boolean Method

package com.softwaretestingo.interviewprograms;
import java.util.Scanner;
public class LeapYearEx3
{
	public static void main(String[] args) 
	{
		boolean leap;
		Scanner sc=new Scanner(System.in);
		System.out.println("Enter a Valid Year: ");
		int year=sc.nextInt();

		if (year % 400 == 0)
            leap = true;

        else if (year % 4 == 0 && year % 100 != 0)
            leap = true;

        else
            leap = false;

        if (leap)
            System.out.println(year + " is a leapyear.");
        else
            System.out.println(year + " is not a leapyear.");
	}
}

Output:

Enter a Valid Year: 2024
2024 is a Leap Year

Conclusion:

In order to check if a year is a leap year, you first need to understand which years are considered leap years. The most basic method to do this would be through if-else statements; however, that could be scoped to any of the other methods mentioned above depending on your specific requirements.

Hope after reading this post you have got an idea how to find an leap years. If still you have questions then you can ask us in the comment section.

I love open-source technologies and am very passionate about software development. I like to share my knowledge with others, especially on technology that's why I have given all the examples as simple as possible to understand for beginners. All the code posted on my blog is developed, compiled, and tested in my development environment. If you find any mistakes or bugs, Please drop an email to softwaretestingo.com@gmail.com, or You can join me on Linkedin.

Leave a Comment