Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

Leap Year Program in Java

Last Updated on August 18, 2023 by Mayank Dham

In this article, we will look at the leap year program in Java. Before we go into the Java program to see if the user-entered year is a leap year or not, let us define a leap year.

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:
The leap year program in Java showcases the application of logical conditions and control structures to determine whether a given year is a leap year or not. By understanding the criteria for a leap year (divisible by 4 but not by 100 unless also divisible by 400), this program demonstrates how programming can solve real-world problems. It exemplifies the importance of precision and accuracy in coding, and how a few lines of code can provide a clear and efficient solution to a specific task. This program serves as a fundamental illustration of how programming languages can be harnessed to solve mathematical and logical challenges, contributing to a deeper comprehension of both programming concepts and the nature of time measurement.

Frequently Asked Questions (FAQs)

Here are some frequently asked questions about the leap year program in Java.

Q1. What is a leap year?
Ans: A leap year is a year that has an extra day, February 29th, making it 366 days long instead of the usual 365. Leap years are introduced to keep our calendar year synchronized with the solar year.

Q2. How does the leap year program work in Java?
Ans: The leap year program in Java typically uses conditional statements to determine if a given year is a leap year or not. It checks whether the year is divisible by 4 (except if it’s divisible by 100 unless also divisible by 400), following the rules of the Gregorian calendar.

Q3. Can you explain the logic behind the leap year program’s conditions?
Ans: The logic ensures that a year divisible by 4 is a potential leap year. However, if the year is divisible by 100, it’s not a leap year unless it’s also divisible by 400. This additional condition compensates for the slight inaccuracy introduced by the rule "every 4 years is a leap year," ensuring accuracy over time.

Q4. What happens if a non-integer input is provided to the program?
Ans: If the program is given a non-integer input, it may produce unexpected results or throw an exception. To handle this, you should include input validation to ensure the user provides a valid year as an integer.

Q5. How can I use the leap year program’s output in my application?
Ans: The output of the leap year program (either "Leap year" or "Not a leap year") can be used in various applications that involve date calculations, event planning, or anything requiring accurate time measurements. For example, you could integrate it into a scheduling application to account for the presence of an additional day in a 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 *