Enter a 4-digit year: 2024
2024 is a Leap Year.
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();
}
}
A year is a leap year if:
It is divisible by 4,
But not divisible by 100,
Except if it is divisible by 400.
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.
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.