Java Program to Check If a Year is Leap Year or Not

  • Write a program in Java to check for leap years.
  • Algorithm to check whether a year is leap year or not.

A leap year contains 366 days instead of the usual 365, by extending February to 29 days rather than the common 28 days. A leap year is a year containing one additional day in order to keep the calendar year in sync with the astronomical year.

It takes the Earth approximately 365.242189 days – or 365 days, 5 hours, 48 minutes, and 45 seconds – to circle once around the Sun. However, the Gregorian calendar has only 365 days in a year, so if we didn't add a leap day on February 29 nearly every four years, we would lose almost six hours off our calendar every year.

Example of leap years: 1980, 1984, 1988, 1992, 1996
Algorithm to check whether a year is leap year or not
  • If a year is divisible by 4 but not by 100, then it is a leap year.
  • If a year is divisible by both 4 and by 100, then it is a leap year only if it is also divisible by 400.
Here is the leap year check condition in one line.
    if(((year%4 == 0) && (year%100 != 0)) || (year%400==0)){
        /* Leap year */
    } else {
        /* Not a leap year */
    }

Java Program to check whether for leap year

package com.tcc.java.programs;

import java.util.*;

public class LeapYearCheck {
    public static void main(String args[]) {
        int year;

        Scanner in = new Scanner(System.in);
        System.out.println("Enter a year");
        year = in.nextInt();
   
        if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
             System.out.println(year + " is a Leap Year");
        else
             System.out.println(year + " is not a Leap Year");
    }
}
Output
Enter a year
2014
2014 is not a Leap Year
Enter a year
2012
2012 is a Leap Year

Recommended Posts
Java Program to Check a Number is Prime Number or Not
Java Program to Check Whether a Number is Armstrong Number or Not
Java Program to Check Whether a Number is Palindrome or Not
Java Program to Check Two Strings are Anagram or Not
Java Program to Check Whether two Strings are Equal or Not
Java Program to Find LCM and GCD of Two Numbers
Java program to convert binary number to decimal number
Java Program to Calculate Volume of Sphere
Java Program for Matrix Multiplication
Java Program to Find Transpose of a Matrix
Java Program to Convert Celsius to Fahrenheit
All Java Programs