Java Program to determine if Given Year is Leap Year

Objective: Given a Year, write a java program to find whether year is leap year or not.

Leap Year: a year, usually occurring once every four years, that has 366 days including February 29 as an intercalary day, called Leap Year.

Example:

Year: 2004, 2008, 2012 are Leap years.
Year: 1993, 2001, 2003 are not leap years.

Approach:

Pseudo code:

if (year is not divisible by 4) then
    (its not a leap year)
else if (year is not divisible by 100) then
    (its a leap year)
else if (year is not divisible by 400) then
    (its not a leap year)
else (it is a leap year)

Java Code:

public class LeapYear {
static void isLeapYear(int year){
if(year<=0){
System.out.println("Invalid year entry");
return;
}
//check for leap year
boolean isLeap;
if(year%4==0){
if(year%100==0){
if(year%400==0){
isLeap = true;
}else{
isLeap = false;
}
}else{
isLeap = true;
}
}else{
isLeap = false;
}
if(isLeap)
System.out.println("Given year " + year + " is leap year");
else
System.out.println("Given year " + year + " is not leap year, its a common year");
}
public static void main(String[] args) {
isLeapYear(1992);
isLeapYear(2000);
isLeapYear(2001);
isLeapYear(2002);
isLeapYear(2004);
}
}

view raw
LeapYear.java
hosted with ❤ by GitHub


Output:

Given year 1992 is leap year
Given year 2000 is leap year
Given year 2001 is not leap year, its a common year
Given year 2002 is not leap year, its a common year
Given year 2004 is leap year