Leap Year Program in Java

In this article, we will study the leap year program in Java. Let’s first define a leap year before we go into the Java program to check if the user-entered year is a leap year or not.

What is a leap year?

Every four years, there is a year known as a leap year. According to the Gregorian calendar, a year should have 365.25 days in it. However, because we count a year as having 365 days, the extra 0.25 days are added to the next year’s calendar, bringing the total to 1 day in the fourth year. Consequently, a leap year has 366 days instead of 365. The extra day is added, making February 29.

How to find a leap year using Java?

You only need to enter a few mathematical conditions with the aid of the If statement in the program code to determine if a year is a leap year or not. The following criteria must be met to determine whether a particular year is a leap year or not:

  • The entering year must divide evenly by four but not by 100.
  • The year entered must divide evenly by 400.

To differentiate between leap years and century years, utilize the second condition. The years ending in ’00 are known as century years, examples are 1300, 1500, 1400, and so on. Only when a century year is equally divisible by 400 is it recognized as a leap year. For instance, since 1200, 1600, and 2000 all precisely divide by 400, they are all century leap years.

Flowchart to Check Leap Year:

Different Method to Check Leap Year

The following are the different method to check the given year is leap year or not.

Method 1: Using if-else statements for Finding Leap Year in Java

The procedure is as follows:

  • Let y be the year.
  • We begin our if-else block by determining if the year is divisible by 400 or not. If it is, the year is a leap year, and we print that result.
  • If the year is not a leap year and the first else if block returns true, we print the result. If not, we go on to the second else if block.
  • In the last else if block, we determine whether the year is divisible by 4, and if it is, we display the result. If that condition also fails, we draw the conclusion that the year is not a leap year.
class LeapYear {

  public static void main(String[] args) {
    // The desired year to check.
    int year = 2000;

    // Implementing our algorithm.
    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.");
    }
  }
}

Method 2: Using the Ternary Operator for Finding Leap Year

As we saw in a prior way, the main goal of a ternary operator in Java is to minimize the number of if-else lines.
A program to discover if a year is a leap or not using ternary operators in Java will need three nested ternary operators, one for each of the conditions indicated in our flowchart:

  • Starting from the left, we determine whether the year is divisible by 4 and not by 100, ensuring that the year is a leap year. If the condition is satisfied, the sentence "is a leap year." is appended to the result string; otherwise, we continue on to the next nested ternary operator.
  • We determine whether the year is divisible by 400 in the final ternary operator. If the condition is met, the phrase "is a leap year" is appended to the result string; if it is false, the phrase "is not a leap year" is appended.
  • Finally, we concatenate and show the outcome.
class LeapYear {

  public static void main(String[] args) {
    // The desired year to check.
    int year = 2003;

    String result;

    result =
      (
        (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 + " " + result);
  }
}

Method 3: Using Methods to Find Leap Year in Java

By creating computing techniques that can be used more freely in multiple different programs, we can make our program modular. The following describes how to identify a leap year using several methods:

  • To do this, we construct a method called checkLeapYear(int) and call it from within our main function, passing it an integer (year) as input and a void return type.
  • To determine if the provided year is a leap year or not and show the outcome, we utilize if-else statements inside the checkLeapYear function.
  • Reminder: Years that are divisible by 400 or by 4 rather than 100 are referred to as leap years.

The process for building a Java application to check for leap years is illustrated below.

class LeapYear {

  // Method 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) {
    // Examples
    checkLeapYear(2000);
    checkLeapYear(2016);
  }
}

Method 4: Using the command line to check for leap year in Java

Command-line arguments are stored as strings in the String array passed to the main method in Java classes. This feature can take input directly while running our program after compilation.

  • In this method, we take the user input directly from the command line when the Java-compiled code is executed.
  • We read arguments from String[] args in the main method using array syntax and convert them into integers by using the Integer class method Integer.parseInt() which converts other data types to integers.
  • We place the input in the checkLeapYear(int year) method and display whether the given year is a leap year or not.

The command would look like, java < classname> < argument>

class LeapYear {

  // Method 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) {
    if (args.length > 0) {
      // convert string into integer
      int year = Integer.parseInt(args[0]);
      checkLeapYear(year);
    } else {
      System.out.println("No arguments provided.");
    }
  }
}

Conclusion:
A similar idea may be used to determine various time periods. Sometimes the if-else statements in Java are introduced using the Java program for the leap year.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *