Google News
logo
Java Program to Check Leap Year
In the following example Java program to check whether a given year is a leap year or not :
Program :
import java.util.Scanner;

public class LeapYearChecker {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a year: ");
        int year = input.nextInt();

        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.");
        }
    }
}
Output :
Enter a year: 2011
2011 is not a leap year.
Enter a year: 2016
2016 is a leap year.
* In this program, we first ask the user to input a year using the Scanner class. Then, we use an if statement to check whether the year is a leap year or not.

* A year is a leap year if it is divisible by 4, but not divisible by 100, unless it is also divisible by 400. We use this logic in our if statement, and print the appropriate message to the console using System.out.println().