Leap Year contains 366 days, which comes once every four years. In this article, we will learn how to write the leap year program in Java.
Facts for Leap Year
Every leap year corresponds to these facts :
- A century year is a year ending with 00. A century year is a leap year only if it is divisible by 400.
- A leap year (except a century year) can be identified if it is exactly divisible by 4.
- A century year should be divisible by 4 and 100 both.
- A non-century year should be divisible only by 4.
Methods for Leap Year Program in Java
There are certain methods for leap year programs in Java are mentioned below:
- Without Using Scanner Class
- Using Scanner Class
- Using Ternary Operator
- Using In-built isLeap() Method
1. Without Using Scanner Class
As the name suggests here the user is bound not to select a year of his choice.
Below is the Java program to implement the approach:
Java
import java.io.*;
public class GeeksforGeeks {
public static void isLeapYear( int year)
{
boolean is_leap_year = false ;
if (year % 4 == 0 ) {
is_leap_year = true ;
if (year % 100 == 0 ) {
if (year % 400 == 0 )
is_leap_year = true ;
else
is_leap_year = false ;
}
}
else
is_leap_year = false ;
if (!is_leap_year)
System.out.println(year + " : Non Leap-year" );
else
System.out.println(year + " : Leap-year" );
}
public static void main(String[] args)
{
isLeapYear( 2000 );
isLeapYear( 2002 );
}
}
|
Output
2000 : Leap-year
2002 : Non Leap-year
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
2000 is a century year divisible by 100 and 4. 2002 is not divisible by 4, therefore not a leap year.
2. Using Scanner Class
Here the user is provided the flexibility to enter the year of their own choice as Scanner Class is imported here rest of the if-else blocks are also combined in a single statement to check if the input year is a leap year.
Below is the Java program to implement the approach:
Java
import java.io.*;
import java.util.Scanner;
public class GFG {
public static void main(String[] args)
{
int year;
Scanner scn = new Scanner(System.in);
year = scn.nextInt();
if ((year % 400 == 0 )
|| ((year % 4 == 0 ) && (year % 100 != 0 ))) {
System.out.println(year + " : Leap Year" );
}
else {
System.out.println(year + " : Non - Leap Year" );
}
}
}
|
Input:
2012
Output:
2012 : Leap Year
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
3. Using Ternary Operator
Ternary Operator is used to reduce the if-else statements. The ternary operator takes three operands, a boolean condition, an expression to execute if the condition is true, and an expression to execute if false.
Below is the Java program to implement the approach:
Java
import java.io.*;
public class GeeksforGeeks {
public static void isLeapYear( int year)
{
boolean is_leap_year = false ;
is_leap_year = (year % 4 == 0 && year % 100 != 0
|| year % 400 == 0 )
? true
: false ;
if (!is_leap_year)
System.out.println(year + " : Non Leap-year" );
else
System.out.println(year + " : Leap-year" );
}
public static void main(String[] args)
{
isLeapYear( 2000 );
isLeapYear( 2002 );
}
}
|
Output
2000 : Leap-year
2002 : Non Leap-year
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
4. Using In-built isLeap() Method
Java has an in-built isLeap() method to check if the input year is a leap year or not.
Below is the Java program to implement the approach:
Java
import java.io.*;
import java.time.*;
import java.util.*;
public class GeeksforGeeks {
public static void isLeapYear( int year)
{
boolean is_leap_year = false ;
Year checkyear = Year.of(year);
is_leap_year = checkyear.isLeap();
if (!is_leap_year)
System.out.println(year + " : Non Leap-year" );
else
System.out.println(year + " : Leap-year" );
}
public static void main(String[] args)
{
isLeapYear( 2000 );
isLeapYear( 2002 );
}
}
|
Output
2000 : Leap-year
2002 : Non Leap-year
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
28 Jul, 2023
Like Article
Save Article