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.
How to determine whether a year is a leap year
- If it is evenly divisible by 4
- Unless it is also evenly divisible by 100
- Unless it is also evenly divisible by 400
Algorithm
- If year is not divisible by 4 then it is a common year
- Else if year is not divisible by 100 then it is a leap year
- Else if year is not divisible by 400 then it is a common year
- 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!!");
}
}
Related Topics