TeachingBee

Leap Year Program In Java

Leap Year Program In Java

In this article, we will see how to check a given year is leap year or not in Java. We will also see various ways to implement Leap Year Program In Java. So,

Let’s dive in.

Problem Statement

Write a Java program to determine whether a given year is a leap year or not.

For Example:

Input: 2024
Output: Leap Year

Input: 1900
Output: Not a Leap Year

Solution

Before proceeding to solution let’s see what actually is a leap year.

What is a Leap Year?

A leap year is a year that contains an extra day, i.e. 366 days in the calendar, whereas a regular or non-leap year has 365 days.

leap year program in java
Leap Year Logic

Rules To Check if Given Year is Leap Year Or Not

  • A year is a leap year if it is exactly divisible by 4.
  • If the year is century year (a year ending with 00), then it is a leap year only if it is divisible by 400.
  • So, A century year should be divisible by 4 and 100 both.
  • A non-century year should be divisible only by 4.

We will see three different ways to check if given year is leap year or not. Those are:

  1. Using if-else
  2. Using Ternary Operators
  3. Using In-built isLeap() Method

Approach 1: Using if-else

Let’s check how to check if given year is leap year or not in java using if-else

Algorithm

  1. Given the input year (year) that you want to check.
  2. Check if it’s a century year (ends with 00):
    • Use the modulo operator % to check if year % 100 == 0.
  3. If it’s a century year, check if it’s divisible by 400:
    • Use the modulo operator % to check if year % 400 == 0.
  4. If it’s not a century year, check if it’s divisible by 4:
    • Use the modulo operator % to check if year % 4 == 0.
  5. If it’s a century year and divisible by 400, it’s a leap year.
  6. If it’s not a century year and divisible by 4, it’s a leap year.
  7. If it’s a century year but not divisible by 400, it’s not a leap year.

Java Program To Check Leap Year Using if-else

// Leap Year Program In Java Using if-else

public class LeapYearChecker {

    public static void main(String[] args) {

        int year = 2024; // Replace this with the year you want to check

        // Check if it's a century year (ends with 00)
        if (year % 100 == 0) {

            // If it's a century year, check if it's divisible by 400
            if (year % 400 == 0) {
                System.out.println(year + " is a Leap Year");
            } else {

                // If it's a century year but not divisible by 400, it's not a leap year
                System.out.println(year + " is not a Leap Year");
            }
        } else {

            // If it's not a century year, check if it's divisible by 4
            if (year % 4 == 0) {
                // If it's not a century year but divisible by 4, it's a leap year
                System.out.println(year + " is a Leap Year");
            } else {

                // If it's not a century year and not divisible by 4, it's not a leap year
                System.out.println(year + " is not a Leap Year");
            }
        }
    }
}

Output

2024 is a Leap Year

Code Explanation

  1. Check if it’s a century year (ends with 00):
    • 2024 % 100 == 24, so it’s not a century year.
  2. Since it’s not a century year, check if it’s divisible by 4:
    • 2024 % 4 == 0, so it’s divisible by 4.
  3. As it’s not a century year but divisible by 4, so it’s considered a leap year.

Approach 2: Using Ternary Operator

Let’s optimise the above code to single line using ternary operator.

Algorithm

  1. Given the input year (year) that you want to check.
  2. Use the ternary operator ? : to perform the leap year check in a single line.
  3. Check if it’s a century year (ends with 00):
    • Use the modulo operator % to check if year % 100 is equal to 0.
  4. If it’s a century year, check if it’s divisible by 400 using the ternary operator:
    • (year % 400 == 0) ? "Leap Year" : "Not a Leap Year"
  5. If it’s not a century year, check if it’s divisible by 4 using the ternary operator:
    • (year % 4 == 0) ? "Leap Year" : "Not a Leap Year"
  6. The ternary operator will return either “Leap Year” or “Not a Leap Year” based on the conditions.

Java Program To Check Leap Year Using Ternary Operator

// Leap Year Program In Java Using if-else

public class LeapYearChecker {
    public static void main(String[] args) {
        int year = 2024; // Replace this with the year you want to check

        // Use the ternary operator to check if it's a leap year
        String result = (year % 100 == 0) ? (year % 400 == 0) ? "Leap Year" : "Not a Leap Year" : (year % 4 == 0) ? "Leap Year" : "Not a Leap Year";

        System.out.println(year + " is " + result);
    }
}

Output

2024 is Leap Year

Approach 3: Using In-built isLeap() Method

Java java.time.Year class in the Java Date and Time API provides an isLeap() method to check if a year is a leap year.

Algorithm

  • Given the input year (year) that you want to check.
  • Create a java.time.Year object using the input year.
  • Use the isLeap() method to check if it’s a leap year:
    • Year.isLeap(year)
  • The method will return true if it’s a leap year and false otherwise.

Java Program To Check Leap Year Using In-built isLeap() Method

// Leap Year Program In Java Using In-built isLeap() Method

import java.time.Year;

public class LeapYearChecker {
    public static void main(String[] args) {
        int year = 2024; // Replace this with the year you want to check

        // Create a Year object and use isLeap() method
        boolean isLeapYear = Year.of(year).isLeap();

        if (isLeapYear) {
            System.out.println(year + " is a Leap Year");
        } else {
            System.out.println(year + " is not a Leap Year");
        }
    }
}

Output

2024 is a Leap Year

Leap Year Program In Java Using Scanner

In all the above code the input year was hard coded. Now, let’s take this input from user via Scanner.

// Leap Year Program In Java Using Scanner

import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        // Create a Scanner object for user input
        Scanner scanner = new Scanner(System.in);

        System.out.print("Enter a year: ");
        int year = scanner.nextInt(); // Read the input year from the user

        // Close the Scanner to avoid resource leaks
        scanner.close();

        // Use the ternary operator to check if it's a leap year
        String result = (year % 100 == 0) ? (year % 400 == 0) ? "Leap Year" : "Not a Leap Year" : (year % 4 == 0) ? "Leap Year" : "Not a Leap Year";

        System.out.println(year + " is " + result);
    }
}

Similarly all the other above methods discussed can be converted to take input from user using Scanner library.

Output

Enter a year: (user enters the year, e.g., 2024)
2024 is Leap Year

Key Takeaways

  • A year is a leap year if it is divisible by 4 but not by 100, or if it is divisible by 400. This is the logic used to determine if a year is a leap year.
  • Using if-else statements, we can check for century year, divisibility by 4 and 400 to implement the leap year logic.
  • Ternary operator can be used to condense the if-else logic into a one-liner expression using modulo and conditional operators.
  • Java’s inbuilt Year class provides an isLeap() method to easily check if a year is a leap year without manual logic.

Similar Posts

Add Days To The Current Date in Java

Calculate Area of Hexagon in Java

Program to Calculate EMI in Java

Calculate Area of a Circle in Java

Reverse A Number In Java

Checkout more Java Tutorials here.

I hope You liked the post ?. For more such posts, ? subscribe to our newsletter. Try out our free resume checker service where our Industry Experts will help you by providing resume score based on the key criteria that recruiters and hiring managers are looking for.

FAQ

What are the advantages of using Java’s inbuilt isLeap() method?

How to check leap year for user input year in Java?

What are the leap years between 1990 to 2022?

Why are leap years needed?

90% of Tech Recruiters Judge This In Seconds! 👩‍💻🔍

Don’t let your resume be the weak link. Discover how to make a strong first impression with our free technical resume review!

Related Articles

ascii value in java

Print ASCII Value in Java

In this article we will into the ASCII table in C, exploring its various character sets—from control characters to printable characters, including letters, digits, and special symbols—and demonstrates how to

data hiding in java

Data Hiding In Java

In this article we will discuss data hiding in Java which is an important concept in object-oriented programming. We will cover So let’s get started. What is Data Hiding in

Why Aren’t You Getting Interview Calls? 📞❌

It might just be your resume. Let us pinpoint the problem for free and supercharge your job search. 

Newsletter

Don’t miss out! Subscribe now

Log In