Java Program to Check Leap Year
A year is a leap year when it satisfies the Gregorian calendar leap-year rules. A year divisible by 4 is normally a leap year, but a year divisible by 100 is not a leap year unless it is also divisible by 400.
For example, 2024 is a leap year because it is divisible by 4 and not by 100. The year 1900 is not a leap year because it is divisible by 100 but not by 400. The year 2000 is a leap year because it is divisible by 400.
In Java, the remainder operator % can be used with an if-else statement to apply these rules. This tutorial first reads a year using Scanner and then checks whether it is a leap year.
Leap Year Conditions Used in Java
A year is a leap year when either of these conditions is true:
- The year is divisible by
400. - The year is divisible by
4but is not divisible by100.
The complete Java condition can therefore be written as:
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
Here, && means logical AND and || means logical OR. The expression year % n == 0 checks whether year is exactly divisible by n.
Algorithm to Check a Leap Year in Java
Following is the algorithm that we shall use to check if given input year is leap year or not.
- Read an integer from the user and store it in the year variable.
- Check whether the year is divisible by
400, or whether it is divisible by4and not divisible by100. - If the condition is true, the given year is a leap year. Otherwise, it is not a leap year.
Leap Year Program in Java Using Scanner and if-else
The following program reads a year from standard input using Scanner. It then evaluates the leap-year condition in an if-else statement.
CheckLeapYear.java
import java.util.Scanner;
/**
* Java Program - Check Leap Year
*/
public class CheckLeapYear {
public static void main(String[] args) {
//read year from user
Scanner scanner = new Scanner(System.in);
System.out.print("Enter year : ");
int year = scanner.nextInt();
//check if year is leap year
if((year%4 == 0 && year%100 != 0) || (year%400 == 0)) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
If the user enters 2024, the first part of the condition is true because 2024 % 4 is 0 and 2024 % 100 is not 0. Therefore, the program identifies 2024 as a leap year.
Output
)
Why Divisibility by 100 and 400 Matters for Leap Years
Checking only year % 4 == 0 is not sufficient for century years. A century year ends in 00 and is divisible by 100. Such a year must also be divisible by 400 to be a leap year.
Consider these common test cases:
2023 - Not a leap year
2024 - Leap year
1900 - Not a leap year
2000 - Leap year
2100 - Not a leap year
2400 - Leap year
The distinction is especially important for 1900 and 2000. Both are divisible by 100, but only 2000 is divisible by 400. Therefore, 1900 is not a leap year, while 2000 is.
Java Leap Year Program Using Nested if-else
The same leap-year rules can be written as nested if-else statements. This form separates the century-year rule from the normal divisible-by-4 rule and can make the decision process easier to follow.
import java.util.Scanner;
public class CheckLeapYear {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter year : ");
int year = scanner.nextInt();
boolean leapYear;
if (year % 400 == 0) {
leapYear = true;
} else if (year % 100 == 0) {
leapYear = false;
} else if (year % 4 == 0) {
leapYear = true;
} else {
leapYear = false;
}
if (leapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
scanner.close();
}
}
In this version, divisibility by 400 is checked first. If that test fails, divisibility by 100 identifies century years that are not leap years. For all remaining years, divisibility by 4 determines the result.
Check Multiple Leap Years in Java Using a for Loop
A for loop is useful when the same leap-year condition needs to be applied to a range of years. The following example prints all leap years from 2020 through 2040.
public class LeapYearsInRange {
public static void main(String[] args) {
for (int year = 2020; year <= 2040; year++) {
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println(year);
}
}
}
}
2020
2024
2028
2032
2036
2040
The loop changes only the value of year. The condition used to identify a leap year remains the same.
Common Mistake When Writing a Java Leap Year Condition
A common mistake is to treat every year divisible by 4 as a leap year:
year % 4 == 0
This condition gives the correct result for many years, but it fails for century years such as 1900 and 2100. Both are divisible by 4, but neither is divisible by 400, so they are not leap years.
Use the complete condition when implementing the Gregorian leap-year rule:
(year % 4 == 0 && year % 100 != 0) || year % 400 == 0
Java Leap Year Program Summary
In this Java Tutorial, we learned how to check if given year is leap year or not. A year is a leap year if it is divisible by 400, or if it is divisible by 4 but not by 100. In Java, these rules can be implemented using the % operator with if-else conditions, and the same test can be used inside a loop when checking multiple years.
TutorialKart.com