Hey there! I’m sure you’ve all heard of the term “Leap Year,” right? A leap year 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 leap year.
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 leap years every four hundred years. The Gregorian calendar is named after Pope Gregory XIII, who introduced this system.
When Is the Next Leap Year?
| Leap Year | Leap Day |
|---|---|
| 2024 | Thursday, February 29 |
| 2028 | Tuesday, February 29 |
| 2032 | Sunday, February 29 |
| 2036 | Friday, 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:
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, depending on your specific requirements, that could be scoped to any of the other methods mentioned above.
I hope that after reading this post, you have an idea of how to find leap years. If you still have questions, you can ask us in the comment section.