Check Leap Year in Java

Check whether a given year is a leap year.

BeginnerModule 2: Conditional ProgramsExample 4 of 20
check-leap-year.java
1import java.util.Scanner;
2
3public class Main {
4 public static void main(String[] args) {
5 Scanner sc = new Scanner(System.in);
6
7 System.out.print("Enter year: ");
8 int year = sc.nextInt();
9
10 boolean isLeap;
11 if (year % 400 == 0) {
12 isLeap = true;
13 } else if (year % 100 == 0) {
14 isLeap = false;
15 } else if (year % 4 == 0) {
16 isLeap = true;
17 } else {
18 isLeap = false;
19 }
20
21 if (isLeap) {
22 System.out.println(year + " is a Leap Year");
23 } else {
24 System.out.println(year + " is not a Leap Year");
25 }
26
27 sc.close();
28 }
29}

Output

Enter year: 2024
2024 is a Leap Year

What's going on

We implement the leap year rules: divisible by 4 and not 100, unless also divisible by 400.