In this Program, we will learn how to Check Leap Year in Java. A leap year is a year that is divisible by 4 and 400 but not by 100.
- Create scanner class object to pass user input.
- Use sc.nextInt() method to store int value inside year parameter.
- we can determine if a year is a leap year by checking the following conditions :
- If the year is divisible by 4 and not by 100, or is evenly divisible by 400, then year is a leap year.
- If none of these conditions are met, then year is not a leap year.
import java.util.Scanner;
public class LeapYearCheck {
public static void main(String[] args) {
System.out.println("Enter the year:");
Scanner sc = new Scanner(System.in);
int year = sc.nextInt();
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
System.out.println("Leap year");
} else {
System.out.println("not leap year");
}
sc.close();
}
}
Output :-
Enter the year:
2012
Leap year