×

Leap Year Program in Java

Leap Year Program in Java: A leap year is a calendar year that have an extra day i.e. 366 days instead of 365 days is called leap year. In other words, A year that is completely divisible by 4 is called leap year. In each leap year, the month of February has 29 days instead of 28. For example, 1998, 1992, 1996, 2020. In this section, we will create a Java program to check the given year is leap year or not.

Algorithm to Check Leap Year

  1. Assign a year to a variable which you want to check.
  2. Now, check the year is completely divisible by 400 or not. If the year is divisible by 400, the given year is a leap year.
  3. If the year is not divisible by 400, then check whether the year is divisible by 4 or not.
  4. If the given year is divisible by 4, then we need to check the year is divisible by 100 or not. If 100 perfectly divides the year then the year is not a leap year otherwise it is a leap year.
  5. If 4 does not divide the year perfectly, then the year is not a leap year.

Let’s implement the above algorithm in a Java program.

Filename: LeapYearExample.java

public class LeapYearExample
{             
private static boolean isLeapYear(int n)
{
               if(n % 400 == 0) // Checking divisibility by 400. 1st step of the algorithm
                               return true;
               //Implementing 3rd and the 4th step of the algorithm
               if((n % 4 == 0) && (n % 100 != 0))
                               return true;
               return false;
}
public static void main(String[] args)
{
               int arr[] = {1998, 1999, 1990, 2000, 2010, 2016, 2020, 2040, 2100};
               for(int i = 0; i < arr.length; i++) //Iterating over each element
               {
               if(isLeapYear(arr[i])) // Checking leap year for each element
                               System.out.println(arr[i] + " is a leap year");
               else
                               System.out.println(arr[i] + " is not a leap year");
               }
}
}

Output:

1998 is not a leap year
1999 is not a leap year
1990 is not a leap year
2000 is a leap year
2010 is not a leap year
2016 is a leap year
2020 is a leap year
2040 is a leap year
2100 is not a leap year

In the above program, we can also implement the logic in a single statement like the following:

if(((year%4==0) && ((year%400==0) || (year%100!==0))  

Explanation: While writing the code, the concept of short-circuiting is used. That is why we have three return statements in the code. Short-circuiting means if, at any point, we can make a decision, we should not be moving further to check other expressions. In our case, if any year is divisible by 400, we can say the year is a leap year. Hence, no need to check its divisibility with 4 or 100, and we can stop right here. If 400 does not divide the year perfectly, we move further and check its divisibility with 4 and 100. If the year is divisible by 4 and not by 100, we have got the leap year; otherwise, the year is not a leap year.


Related Topics

Java Math floorMod() Method

The floorMod() method of Math class returns the floor modulus of the specified arguments. It firstly divides the dividend and divisor and then returns an integer that is equal to...

1 minute read.

Concurrent Linked Deque in Java with Examples

Introduction Java's concurrent-linked deque, which holds its items as linked nodes, is unconstrained and thread-safe. Concurrent Linked Deque allows for element removal and addition on both sides because it implements the...

4 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

How to uninstall the Java in Ubuntu

We need java on our computers in our daily lives because there are lots of different applications that are developed using the java environment, so we have to install java...

4 minutes read.

Nth Term of Geometric Progression in Java

There are 3 numbers provided. The geometric progression's initial term is the first number. The second number is the geometric progression's common ratio, and the third number represents the nth...

3 minutes read.

Iterate JSON array in Java

This article is going to equip the knowledge about what JSON array is and how it works and also how is it distinct with the generic array. Json Array Ordered lists of...

4 minutes read.

Java Extends vs Implements

Java: We known that the java is a pure object oriented programming language. Java programming language consists of many features such as portable, plat form independence, secured, robust, simple, architecture neutral,...

4 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

How to achieve Multiple Inheritance in Java

Multiple Inheritance is the type of inheritance where one class inherits the properties of more than one super class. For example, class Child inherits (extends) the classes Father and Mother....

4 minutes read.

Java Arrays Fill

We may use the Arrays.fill () function to fill a whole array or a subset of it. Arrays.fill () may fill both 2D and 3D arrays. Syntax: Arrays.fill(boolean[] fillArr, int fromIndex, int toIndex, boolean val )   Parameters: The array to be filled...

4 minutes read.

Java program to determine whether all leaves are at same level

In this program, we must determine whether or not all of the binary tree's leaves are at the same level. If a node has no child nodes, it is said to...

3 minutes read.

Decagonal Numbers in Java

This section explains what is a decagonal number and how to write Java programmes that compute decagonal numbers. Both academics and Java programmer interviews regularly question about the Decagonal number...

3 minutes read.

Java Xml Parser

In this article, we acknowledge you about what is a XML parser in Java, how is it going to work and what is the purpose of it. Also, the article discusses...

6 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Java Thread Dump Analyzer

Thread: A thread is a PC program that is stacked into the PC's memory and is under execution. It tends to be executed by a processor or a bunch of processors....

15 minutes read.

Lazy Propagation in Segment Tree in Java

The topic of segment trees in Java is continued by the topic of sluggish propagation in segment trees. It is suggested that readers first read through the section tree topic....

4 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Switch Case with Enum in Java

From some conditions, the java switch statement executes one statement. Similar to the If-Else-If ladder statement, this can be Byte, short, int, long, enum, string, and some wrapper types like...

4 minutes read.

Java String toCharArray() method

Java String toCharArray() method converts current String into character array. Syntax: public char[] toCharArray() Returns: It returns a character array Java String toCharArray() method example 1 import java.util.Arrays; public class JavaStringToCharArrayEx1 {           public static...

2 minutes read.