Java program to check leap year
In this post, you will learn different ways in Java programming to check whether a particular year is a leap year or not. A leap year is 366 days old instead of 365 days because February is about 29 days longer than the common 28 days. These extra days occur in years which are multiples of four, i.e., 1992, 1996, 2000, 2004, 2008, 2012.
Logic for the Leap Year
Most of us definitely know the logic behind checking for a leap year in a Gregorian calendar. These are the three rules that should be followed to identify a leap year-
- The year can be evenly divided by 4,
- If the year can be evenly divided by 100, it is NOT a leap year, except if;
- The year is also evenly divisible by 400. Then it is a leap year.
Check leap year using if else statement in Java
In the given Java program, we are checking if the year 2012 is a leap year or not. As 2012 is divisible by 4, it is a leap year.
public class CheckLeapYear {
public static void main(String[] args) {
int year = 2012;
boolean leap = false;
// leap year if the year is evenly divided by 4
if (year % 4 == 0) {
// leap year if the year is evenly divided by 100
if (year % 100 == 0) {
// leap year if evenly divisible by 400
if (year % 400 == 0)
leap = true;
else
leap = false;
}
// if the year is not century
else
leap = true;
}
else
leap = false;
if (leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
Output of the above code:
2012 is a leap year.
Check leap year using logical operator in Java
In the above example, we have used nested if-else statements to check for the leap year. We can also use the logical operator to reduce the number of if-else statements.
public class CheckLeapYear {
public static void main(String[] args) {
int year = 2020;
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0))){
System.out.println(year + " is a leap year.");
}else{
System.out.println(year + " is not a leap year.");
}
}
}
Output of the above code:
2020 is a leap year.
Check leap year using ternary operator in Java
We can totally eliminate the use of the if-else statement by using the ternary operator to check the leap year in Java. We need three nested ternary operators, one for each condition as mentioned above. The given example demonstrates this.
public class CheckLeapYear {
public static void main(String[] args) {
int year = 2018;
String output;
output = ((year % 4 == 0 && year % 100 != 0) ?
"is a leap year." : (year % 400 == 0) ?
"is a leap year." : "is not a leap year.");
// Append the year and format the string
System.out.println(year + " " + output);
}
}
Output of the above code:
2018 is not a leap year.
Check leap year using function in Java
Here is the other approach to check leap year in Java by defining a function. This is a more flexible way to check the leap year than the above approaches.
public class CheckLeapYear {
// function to check leap year
public static void checkLeapYear(int year) {
if (year % 400 == 0) {
System.out.println(year + " is a leap year.");
} else if (year % 100 == 0) {
System.out.println(year + " is not a leap year.");
} else if (year % 4 == 0) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
public static void main(String[] args) {
// Calling function
checkLeapYear(2010);
checkLeapYear(1994);
checkLeapYear(2020);
}
}
Output of the above code:
2010 is not a leap year.
1994 is not a leap year.
2020 is a leap year.
Related Articles
Capitalize first letter of each word JavaConvert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
nth prime number in Java
Determinant of a matrix in Java
Java program to count the occurrences of each character
Java program to find area of rectangle
Vowel and Consonant program in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to find factorial of a number