In this program we will write a Java program to check whether a year is a leap year or not. 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 the logic to find whether a year is a leap year or not.
The rule states:
“Every year that is exactly divisible by four 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 example,
if year = 2400, it is leap year,(Condition 1 satisfied)
but if year = 2200, is NOT a leap year, (Cond. 2 not satisfied),
and if year = 2020, is a leap year, (Cond. 2 satisfied).
To check whether a year is a leap year or not, you need to check the following 3 conditions:
IF
1. Any year that is divisible by 400 is definitely a leap year.
ELSE IF
2. If divisible by 4 AND not divisible by 100 then a leap year.
ELSE
not a leap year.
Java program to check LEAP year –
public class JavaApplication4 { public static void main(String[] args) { int year = 2020; if(year%400==0) { System.out.println("Leap Year"); } else if(year%4==0 && year%100!=0) { System.out.println("Leap Year"); } else { System.out.println("Not a Leap Year"); } } }