How to Check Leap Year in Java?

How to Check Leap Year in Java?

3 mins read38.4K Views Comment
clickHere
Atul
Atul Harsha
Senior Manager Content
Updated on Sep 15, 2023 17:49 IST

In this blog, you will learn how to check leap year in Java. A leap year is a year that is divisible by 4 and 400 but not by 100.

2023_01_Pattern-Program-in-Python-1.jpg

For example, the year 2000 is a leap year because it is divisible by 4 and by 400. But the year 1900 is non leap year because it is not divisible by 400. In this tutorial, we will learn how to write a leap year program in Java and check if a given year is a leap year or not. We will use a series of if clauses and boolean operators to check the divisibility of the year by 4, 100, and 400, and use the rules for leap years to determine if the year is a leap year or not.

Problem: Write a Java program that reads a year from the user and determines if it is a leap year.

To determine if a year is a leap year, the following rules are used:

  1. If the year is evenly divisible by 4, go to step 2.
  2. If the year is evenly divisible by 100, go to step 3.
  3. If the year is evenly divisible by 400, the year is a leap year.
  4. If the year is not evenly divisible by 100 or is evenly divisible by 400, the year is a leap year.
  5. If none of the above conditions are met, the year is not a leap year.

Using these rules, you can determine if a year is a leap year by checking the following conditions:

  • If the year is evenly divisible by 4 and not by 100, or is evenly divisible by 400, the year is a leap year.
  • If none of these conditions are met, the year is not a leap year.

Explore popular Java Courses

Solution:


 
public class Main {
public static void main(String[] args) {
int year = 2020;
boolean isLeapYear;
// divisible by 4
isLeapYear = (year % 4 == 0);
// divisible by 4, not by 100, or divisible by 400
isLeapYear = isLeapYear && (year % 100 != 0 || year % 400 == 0);
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
Copy code

This code will output “2020 is a leap year.”

This code uses the following logic to determine if a year is a leap year:

  • Check if the year is evenly divisible by 4. If it is, set isLeapYear to true else isLeapYear remains false.
  • Check if the year is evenly divisible by 100 and not by 400. If it is not, set isLeapYear to true. If it is, isLeapYear remains false if it was previously set to false, or unchanged if it was previously set to true.
  • If isLeapYear is true, the year is a leap year else the year is not a leap year.
Prime Number in Java: How to Check for Primality and Generate Prime Numbers
Prime Number in Java: How to Check for Primality and Generate Prime Numbers
This article will aid knowledge in your programming journey to analyze Java’s primality and prime numbers.
Master the Concept of Recursion Java
Master the Concept of Recursion Java
This blog will make you understand the concept of Recursion in Java with the help of several problems.
Java Methods Explained (with Code)
Java Methods Explained (with Code)
The below java tutorial covers java methods with examples and explanations. It also goes through the declaration, implementation, types, and its uses.

You can optimize the above code as:


 
public class Main {
public static void main(String[] args) {
int year = 2020;
// divisible by 4, not by 100, or divisible by 400
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.");
}
}
}
Copy code

Unlock the secrets of Java with our career prospects and comprehensive guide; discover information on top collegesspecialised programmes and online courses to excel in the world of Java!

Check Leap Year in Java using Ternary Operator

The ternary operator takes three operands: a boolean condition, an expression to execute if condition is true, and an expression to execute if false.

  • Use boolean condition year % 4 == 0 && year % 100 != 0 || year % 400 == 0.
  • If the boolean condition is true, you can use the ternary operator to return the string “is a leap year”. If the condition is false, the operator can return the string “is not a leap year”.
  • Finally print whether the year is a leap year or not.
2023_01_image-16.jpg

 
public class Main {
public static void main(String[] args) {
int year = 2020;
String result;
// divisible by 4, not by 100, or divisible by 400
result = (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) ? "is a leap year" : "is not a leap year";
System.out.println(year + " " + result + ".");
}
}
Copy code

Check if the Year Entered by User is a Leap Year

Sarah is a software developer who has been asked to write a program to determine if a given year is a leap year in Java. She has been given the following requirements for the program:

  • The program should prompt the user to enter a year.
  • The program should read the user’s input and determine if the entered year is a leap year.
  • The program should print out a message indicating whether the year is a leap year or not.

Sarah begins by researching the rules for determining if a year is a leap year. She learns that a year is a leap year if it is evenly divisible by 4, unless it is evenly divisible by 100 but not by 400.

Solution:


 
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
//Enter the year as input
System.out.print("Enter a year: ");
int year = scanner.nextInt();
boolean isLeapYear;
// divisible by 4
isLeapYear = (year % 4 == 0);
// divisible by 4, not by 100, or divisible by 400
isLeapYear = isLeapYear && (year % 100 != 0 || year % 400 == 0);
if (isLeapYear) {
System.out.println(year + " is a leap year.");
} else {
System.out.println(year + " is not a leap year.");
}
}
}
Copy code
About the Author
author-image
Atul Harsha
Senior Manager Content

Experienced AI and Machine Learning content creator with a passion for using data to solve real-world challenges. I specialize in Python, SQL, NLP, and Data Visualization. My goal is to make data science engaging an... Read Full Bio