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 Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 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.

📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.