Java Program to Find if a Given Year is a Leap Year
Leap Year contains 366 days, which comes once every four years. Every leap year corresponds to these facts :
- A century year is a year ending with 00. A century year is a leap year only if it is divisible by 400.
- A leap year (except a century year) can be identified if it is exactly divisible by 4.
- A century year should be divisible by 4 and 100 both.
- A non-century year should be divisible only by 4.
Let’s find out whether a year is a leap year or not.
Without Using Scanner Class
As the name suggests here user is bound not to select a year of his choice. The following program illustrates the way to find out if a year is a leap year:
2000 : Leap-year 2002 : Non Leap-year
2000 is a century year divisible by 100 and 4. 2002 is not divisible by 4, therefore not a leap year.
Using Scanner Class
Here the user is provided the flexibility to enter the year of own choice as Scanner Class is imported here rest the if-else blocks are also combined in a single statement to check if the input year is a leap year. The following program illustrates the way to find out if a year is a leap year:
2012 : Leap Year
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.