×

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 Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Transaction Management in java

Definition: A database application is an application that is running against a relational database and executes one or more transactions. A transaction is an executing program that contains some database operations,...

4 minutes read.

Kotlin Vs Java

Kotlin Vs Java There are many languages available for Android development. Java is the official language for android development but Kotlin is becoming popular nowadays. This article discusses both of these...

4 minutes read.

How to Install Java on MAC

There are many possible ways to install java on mac. This article is based on the installation of java on mac. The operating system platform is Mac OS X, macOS and...

3 minutes read.

Java Continue Keyword

The java keyword ‘continues’ has also been called as java continue statement.The main concepts of the java continue statement or keyword is used in the controlling of the loop structure.Java...

3 minutes read.

Collection Interfaces in Java with Examples

In this tutorial, we will discuss collection interfaces in Java with Examples. Introduction The Collection Interface is an individual from the Java Collections Framework. It is a root point of interaction of...

4 minutes read.

Java import packages

To know about the importing the packages of Java, we need to understand about how to packages work. Packages The package in Java is a collection of Classes and Interfaces. The packages...

3 minutes read.

Java Short Keyword

Java supports eight different primitive datatypes. The language has predefined primitive datatypes that are given keyword names. Let's take a closer look at each of the eight primitive data types....

3 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

Java finally

Java finally: There are some statements in a program whose execution is extremely important. For example, closing of a database connection. Statements that do the closing of the database connection...

5 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Generic queue in Java

Before understanding how to implement a generic queue in java, one must know about generics and queue in java. Generics in Java Generics are parameterized types. The goal is to enable type...

6 minutes read.

Java Class Syntax

A class is a fundamental building piece in object-oriented programming. It can be characterised as a template that outlines the information and actions connected to the creation of a class....

3 minutes read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java logger

Logging is a crucial Java feature that aids developers in identifying issues. The logging methodology is included with Java as the programming language. It offers the Logging API, which debuted...

7 minutes read.

Java Pop

The array, linked list, stack, queue, and other data structures are supported by Java programming. The insertion, deletion, and element searching operations are available for every data structure. And Java...

4 minutes read.

Java vs Golang

Java Java is both a programming language and a platform. Java is a high-level, dependable, object-oriented, and secure programming language. Java was developed in 1995 by Sun Microsystems, which is now an...

4 minutes read.

How to Return Value from Lambda Expression Java?

What is Lambda Expression in Java? In Java 8, Lambda Expressions were introduced.A lambda expression is a brief section of code that accepts input and outputs a value. Similar to methods,...

4 minutes read.

Java Stringjoiner Class

StringJoiner is a class which is used to construct a sequence of characters which are separated by a delimiter. Optionally, it starts with a provided prefix and ended with the...

5 minutes read.