Check Leap Year in Java
Here you will get the program code to check leap year in java programming.
A leap year is a year that has an extra day, February 29th, to keep our calendar in alignment with the Earth’s revolutions around the Sun, occurring every four years.
Program to Check Leap Year in Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | import java.util.Scanner; public class CheckLeapYear { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter a year : "); int year = scanner.nextInt(); scanner.close(); boolean isLeapYear = false; if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) { isLeapYear = true; } if (isLeapYear) { System.out.println(year + " is a leap year."); } else { System.out.println(year + " is not a leap year."); } } } |
Output
C:\CodeRevise\java>javac CheckLeapYear.java
C:\CodeRevise\java>java CheckLeapYear
Enter a year : 2001
2001 is not a leap year.
C:\CodeRevise\java>java CheckLeapYear
Enter a year : 2004
2004 is a leap year.
C:\CodeRevise\java>java CheckLeapYear
Enter a year : 2020
2020 is a leap year.
Check out our other Java Programming Examples