A Leap Year is a year which has 366 days. This year February is of 29 days. Leap years occurs in every four years.
A leap year has following constraints.
We can check in programming as below
- A year is divisible by 400 then it is a leap year
- A year is divisible by 4 and not divisible by 100 then the year is leap year
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | import java.util.Scanner; public class MyClass { public static void main(String args[]) { System.out.println("Enter the year to check it is prime or not"); Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); if (year % 4 == 0) { if (year % 100 == 0) { if (year % 400 == 0) { System.out.println("Year " + year + " is a leap year"); } else { System.out.println("Year " + year + " is not a leap year"); } } else { System.out.println("Year " + year + " is a leap year"); } } else { System.out.println("Year " + year + " is not a leap year"); } } } |
1 2 3 | Enter the year to check it is prime or not 2020 Year 2020 is a leap year |
See another program
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import java.util.Scanner; public class LeapYearTest { public static void main(String args[]) { System.out.println("Enter the year to check it is prime or not"); Scanner scanner = new Scanner(System.in); int year = scanner.nextInt(); if ((year % 400 == 0) || ( ( year % 100 != 0) && (year % 4 == 0 ))){ System.out.println("Year " + year + " is a leap year"); } else { System.out.println("Year " + year + " is not a leap year"); } } } |
1 2 3 | Enter the year to check it is prime or not 2000 Year 2000 is a leap year |