Java Program for Leap Year

leap year java program
Reading Time: 7 minutes

Introduction:

The “LeapYearChecker” Java program is designed to determine whether a given year is a leap year or not. A leap year is one that has 366 days, including an extra day in February, and occurs approximately every four years. This program uses a simple algorithm to evaluate the leap year conditions and provides a user-friendly interface for inputting the year to be checked.

Explanation:

The program begins by prompting the user to input a specific year through the console using the Scanner class. Subsequently, the entered year is passed to the isLeapYear function, where the leap year conditions are evaluated. The logic for determining a leap year is based on two main criteria:

  1. The year must be divisible by 4.
  2. If the year is divisible by 100, it should not be divisible by 400, unless it is divisible by both.

If these conditions are met, the program outputs that the entered year is a leap year; otherwise, it indicates that the year is not a leap year.

The use of the Scanner class facilitates user interaction, allowing for dynamic input, making the program versatile and applicable to various scenarios.

Algorithm:

  1. Begin the program by importing the Scanner class for user input.
  2. Create a class named LeapYearChecker.
  3. Inside the main method:
    • Instantiate a Scanner object to receive user input.
    • Prompt the user to enter a year.
    • Read and store the entered year in an integer variable.
  4. Call the isLeapYear function, passing the entered year as an argument.
  5. Inside the isLeapYear function:
    • Check if the year is divisible by 4.
    • If divisible by 4, check if it is not divisible by 100 or if it is divisible by 400.
    • Return true if the conditions are met, indicating a leap year; otherwise, return false.
  6. In the main method, based on the result from the isLeapYear function:
    • Print a message indicating whether the entered year is a leap year or not.
  7. Close the Scanner object to release resources.

This program provides a clear and concise method for determining leap years in Java, combining functionality with user interaction to enhance its usability.

Explanation:

    
     import java.util.Scanner;
public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();
        if (isLeapYear(year)) {
            System.out.println(year + " is a leap year.");
        } else {
            System.out.println(year + " is not a leap year.");
        }
        scanner.close();
    }
    // Function to check if a year is a leap year
    public static boolean isLeapYear(int year) {
        // Leap year is divisible by 4, but not divisible by 100 unless it is divisible by 400
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
}

    
   

Input :

    
     Enter a year: 2024
    
   

Output :

    
     2024 is a leap year
    
   

You can run this program, and it will prompt you to enter a year. Then it will tell you whether the entered year is a leap year or not.

FAQ’s :

Q1: How can I write a Java program for leap year detection?

A1: You can create a Java program to check for leap years by defining a function that implements the leap year logic. This typically involves checking if the year is divisible by 4, not divisible by 100 unless it’s divisible by 400.

Q2: Could you provide a Java program to check if a given year is a leap year?

A2: Certainly! Here’s an example Java program that takes a year as input and checks whether it’s a leap year or not.

Q3: How do I create a Java program for leap year using the Scanner class for user input?

A3: You can use the Scanner class to take user input for the year

Q4: What is the Java program logic for determining whether a year is a leap year or not?

A4: The logic for determining whether a year is a leap year in a Java program involves checking if the year is divisible by 4, not divisible by 100 unless it is divisible by 400.