Java Program to Check Leap Year or not
Leap year or not using java :
Leap year is a year which is completely divisible by 4, but the year should not be a century year except when the year is divisible by 400. For better understanding let’s consider an example.
Example : Suppose a year is 2016,
Then, check that it should be completely divisible by 4 and should not be a century year or can say that the year should not be completely divisible by 100 .
Yes, 2016 is completely divisible by 4 and not a century year, because it is not completely divisible by 400.
And now if the above condition do not gets true then check that the year is completely divisible by 400 or not.
If either of the above conditions get true, then the year is a Leap Year, otherwise the year is not a Leap Year
Some examples of Leap Year are :
1200 , 1400 , 2000 , 2012 , 2016 , 2020 , 2024 ,….etc.
So, in this article we will create a java program to check whether the year entered by user is a Leap Year or not.
Step 1 : Ask the user to enter a year.
Step 2 : Use the condition that year entered by user should be divisible by 4,but the year should not be a century year except it is divisible by 400.
Step 3 : If the above condition get true then the year is a Leap Year, otherwise the year is not a Leap Year.
Step 4 : Display the result.
Code in Java :
//Java program to check whether a year entered by user is a leap year or not
public class LeapYear
public static void main(String args)
//scanner class declaration
Scanner sc=new Scanner(System.in);
//input year from user
System.out.println("Enter a Year");
int year = sc.nextInt();
//condition for checking year entered by user is a leap year or not
if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
System.out.println(year + " is a leap year.");
System.out.println(year + " is not a leap year.");
//closing scanner class(not compulsory, but good practice)
Enter a Year
1300 is not a leap year.
Enter a Year
2020 is a leap year.