Java Program To Check Leap Year
- May 11, 2020
- by

Java Program To Check Leap Year
In this Java Program, we check whether the year entered by the user is a leap year or not.
The year which is exactly divisible by 4 is a Leap Year, except for years that are exactly divisible by 100, but these centurial years are leap years if they are exactly divisible by 400.
For e.g. the years 1700, 1800 and 1900 are not leap years, but the years 1600 and 2000 are leap years.
Logic : if(year % 4 == 0)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}
Step 1 : Import java.util.Scanner
Step 2 : Create a variable named year.
Step 3 : Ask the user to enter the year by using System.out.println method.
Step 4 : Store the user entered year in the variable year.
Step 5 : Close the Scanner to avoid memory leak.
Step 6 : Apply the boolean isLeap = false
Step 7 : Apply the if loop
Step 8 : Display the result.
Program :
import java.util.Scanner;
public class LeapYear
{
public static void main(String[] args)
{
int year;
Scanner sc = new Scanner(System.in);
System.out.println("Enter any Year:");
year = sc.nextInt();
sc.close();
boolean isLeap = false;
if(year % 4 == 0)
{
if(year % 100 == 0)
{
if(year % 400 == 0)
isLeap = false;
}
else
isLeap = true;
}
else {
isLeap = false;
}
if(isLeap == true)
System.out.println(year +"is a Leap Year");
else
System.out.println(year + "is not a Leap Year");
}
}
Output:
Enter any Year:
2001
2001 is not a Leap Year.
For execution of the same do visit my YouTube Channel:

0 comments:
Post a Comment