In this tutorial, we will learn how to check if leap year java. A leap year is a special year which has 1 day extra. This normal years have 365 days, however a leap year has 366 days. A leap year comes every 4 years.
Here is a Java Program to check Leap year, A leap year has 366 days, instead of 365. To check if leap year java is a leap year or not first we have to find out the possible ways of checking a leap year mathematically. The following conditions have to be fulfilled:
- The year is a leap year if it has 366 days.
- It is not if it has 365 days.
- The year is evenly divisible by 400, then check for step 1, else move to step 2.
- If it is evenly divisible by 100, check for step 3, else move to step 1.
- The year is evenly divided by 4, check for step 4, else move to step 2.
Steps to Check Leap Year
- Take an integer variable as input for the year.
- Assign a particular value to the variable.
- We first check if the year is divisible by 4, but not 100, simply print the output as The year is a leap year.
- If the input is completely divisible by 400, simply print the same output.
- Else, we print βThe year is not a leap yearβ.
Java Code to Check Leap Year by Giving Input
public class LeapYear {
public static void main(String[] args) {
int year = 2023;
boolean Leap = false;
if(year % 4 == 0)
{
if( year % 100 == 0)
{
if ( year % 400 == 0)
Leap = true;
else
Leap = false;
}
else
Leap = true;
}
else {
Leap = false;
}
if(Leap==true)
System.out.println(year + " is a Leap Year.");
else
System.out.println(year + " is not a Leap Year.");
}
}
Learn Scanner Class in Java
The output of the program will be:
2023 is a leap year
Check Leap Year
Algorithm
- Start
- Declare a variable let’s say a year.
- Initialize it.
- Check for the conditions.
- If the condition satisfies then it is a leap year else not.
- Display the result.
- Stop.
This is the whole elaborated version of how to understand the algorithm. To reduce the complexity of the program, we can also write is as following:
Code :
public class DeveloperHelps {
public static void main(String[] args)
{
//year to leap year or not
int year = 2020;
System.out.println();
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
System.out.println(year + " is a leap year");
else
System.out.println(year + " is not a leap year");
System.out.println();
}
}
Output :
2020 is a leap year.
Check Leap Year using Conditional Operators
Algorithm
- Start
- Declare a variable let’s say a year.
- Initialize it.
- Use a ternary operator to check whether the given year is a leap year or not.
- Call a method in the condition section of the ternary operator to check the same.
- Return true if the year is a multiple of 400.
- Else if the year is a multiple of 100, return false.
- Else if the year is a multiple of 4 then it is a leap year and return true. Else return false.
- Stop.
Code :
//Java Program to check whether the given year is a leap year or not
import java.util.Scanner;
public class CheckYear
{
static boolean checkLeapYear(int year)
{
// If a year is multiple of 400, then it is a leap year
if (year % 400 == 0)
return true;
// Else If a year is multiple of 100, then it is not a leap year
if (year % 100 == 0)
return false;
// Else If a year is multiple of 4, then it is a leap year
if (year % 4 == 0)
return true;
return false;
}
// Driver method
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int year; //Year Declaration
System.out.println("Enter the year");
year=sc.nextInt(); //Year Initialization
//Ternary Operator to check
System.out.println( checkLeapYear(2000)? "Leap Year" :
"Not a Leap Year" );
}
}
Output :
Enter the year 2012
Leap Year
Java Programs with Examples and Output
Thanks for the reading post. I hope you like and understand the post. If you have any doubts regarding this post please comment below.