Home / Programs / Write a Java program to find out whether a year (entered in 4-digit number representing it) is a leap year.
Programming Example

Write a Java program to find out whether a year (entered in 4-digit number representing it) is a leap year.

👁 9 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Given Input:

Enter a 4-digit year: 2024

Expected Output:

2024 is a Leap Year.

Program Code

import java.util.Scanner;

public class LeapYearCheck {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a 4-digit year: ");
        int year = sc.nextInt();

        // Check if year is 4-digit
        if (year < 1000 || year > 9999) {
            System.out.println("Invalid input! Please enter a 4-digit year.");
        } 
        else {
            // Leap year condition
            if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
                System.out.println(year + " is a Leap Year.");
            } else {
                System.out.println(year + " is NOT a Leap Year.");
            }
        }

        sc.close();
    }
}

Explanation

Leap Year Logic

A year is a leap year if:

  • It is divisible by 4,

  • But not divisible by 100,

  • Except if it is divisible by 400.

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.