Java program to check leap year
Write a java program to check 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
- Program:
public class Main {
public static void main(String[] args) {
// year to be checked
int year = 2011;
boolean leap = false;
// if the year is divided by 4
if (year % 4 == 0) {
// if the year is century
if (year % 100 == 0) {
// if year is divided by 400
// then it is a leap year
if (year % 400 == 0)
leap = true;
else
leap = false;
}
// if the year is not century
else
leap = true;
}
else
leap = false;
if (leap)
System.out.println(year + " is a leap year.");
else
System.out.println(year + " is not a leap year.");
}
}
- Output:
2011 is not a leap year--- Another example ---2012 is a leap year
0 comments