How to check if a given year is a leap year or not using java

import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = scanner.nextInt();
        boolean isLeapYear = checkLeapYear(year);
        System.out.println(year + " is " + (isLeapYear ? "a leap year" : "not a leap year"));
        scanner.close();
    }

    private static boolean checkLeapYear(int year) {
        if (year % 4 != 0) {
            return false;
        } else if (year % 100 != 0) {
            return true;
        } else {
            return year % 400 == 0;
        }
    }
}

 

Post your Answer