Monday, 21 February 2011

The Leap Year Rule

This is the algorithm to use when working out if a year is a leap-year or not...

IF the year MOD 400 equals 0
THEN its a leap year
OR IF the year MOD 4 equals 0
AND the year MOD 100 does not equal 0
THEN its a leap year


The code to implement the above goes something like this:

  public static boolean isLeapYear(int year) {

   
boolean leapYear = false;

   
if (((year % 400) == 0) || (((year % 4) == 0) && ((year % 100) != 0))) {
     
leapYear = true;
   
}

   
return leapYear;
 
}

No comments: