A Leap Year is a year that has 366 days. This year February is of 29 days. Leap years occur in every four years.
A leap year has the following constraints.
We can check Leap Year Program in Java 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 a leap year
Q Write a Leap Year Program in Java
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 |
Q write a program to decide whether input year is leap year or not.
See another program. Here Scanner class is used to take value from the user.
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 |
Q Write a program to check the given year 2004 leap year or not
To check this pass the value to the above program it will produce the following data
1 2 3 | Enter the year to check it is prime or not 2000 Year 2004 is a leap year |