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

Java Programming Language Decision Making in java (Article) Decision Making in java (Program)

6

Given Input:

Enter a 4-digit year: 2024

Expected Output:

2024 is a Leap Year.

Program:

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();
    }
}

Output:


                                        

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.


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.