Java Program to Check Leap Year

What is a Leap Year?

It takes approximately 365.25 days for Earth to orbit the Sun (a solar year). We usually round the days in a calendar year to 365. To make up for the missing partial day, we add one day to our calendar approximately every four years . This mean that, a year containing an extra day. It has 366 days instead of the normal 365 days. That is a leap year. The extra day is added in February, which has 29 days instead of the normal 28 days.
leap year in Java

How to determine whether a year is a leap year

  1. If it is evenly divisible by 4
  2. Unless it is also evenly divisible by 100
  3. Unless it is also evenly divisible by 400
The year 2000 was a leap year, because it obeys all three rules 1, 2, and 3, and therefore was a leap year.

Algorithm

  1. If year is not divisible by 4 then it is a common year
  2. Else if year is not divisible by 100 then it is a leap year
  3. Else if year is not divisible by 400 then it is a common year
  4. Else it is a leap year
import java.util.Scanner; public class test { public static void main(String[] args) { int inYear; Scanner scanYear = new Scanner(System.in); System.out.print("Enter a Calendar Year:"); inYear = scanYear.nextInt(); scanYear.close(); boolean leapYear = false; if(inYear % 4 == 0) { if( inYear % 100 == 0) { if ( inYear % 400 == 0) leapYear = true; else leapYear = false; } else leapYear = true; } else { leapYear = false; } if(leapYear==true) System.out.println(inYear + " is a Leap Year!!"); else System.out.println(inYear + " is not a Leap Year!!"); } }