Check whether given year is Leap year or not
A leap year in the Gregorian calendar has an extra day for February. A leap year has 366 days instead the 365 days of a common year. Leap year is a multiple of ‘4’. Leap year has 29 days for February. Normal year has 28 days for February
Leap year: 366 days, 52 weeks + 2 odd days
Non-Leap year: 365 days, 52 weeks + 1 odd day
Leap year: 366 days, 52 weeks + 2 odd days
Non-Leap year: 365 days, 52 weeks + 1 odd day
The standard logic to determine whether a year is leap year or not is: If the year is divisible by 4 and is not divisible by 100 or the year is divisible by 400 then it’s a leap year.
PROGRAM
PROGRAM
class LeapYear {
public static void main(String[] args) {
int year = Integer.parseInt(args[0]);
boolean isLeapYear = false;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
{
System.out.println("Year "+year+" is a Leap Year");
}
else
{
System.out.println("Year "+year+" is not a Leap Year");
}
}
}
OUTPUT 1
C:\>javac LeapYear.java C:\>java LeapYear 2000 Year 2000 is a Leap YearOUTPUT 2
C:\>javac LeapYear.java C:\>java LeapYear 2015 Year 2015 is not a Leap Year