Skip to main content

Leap Year

Let's check if a year is a leap year.

Rules for determining a Leap Year

  • Most years that can be divided evenly by 4 are leap years. (For example, 2016 divided by 4 = 504: Leap year!)
  • Exception: Century years are NOT leap years UNLESS they can be evenly divided by 400. (For example, 1700, 1800, and 1900 were not leap years, but 1600 and 2000, which are divisible by 400, were.)

Here we have different solutions for our short program.

PHP: using the DateTime object:


$year = 2000;
$isLeap = DateTime::createFromFormat('Y', $year)->format('L') === "1";
var_dump($isLeap); // bool(true)

Python: a clean and short function


def is_leap_year(year):
   """Determine whether a year is a leap year."""
   return year % 4 == 0 and (year % 100 != 0 or year % 400 == 0)

""" Test """
year = int(input("Enter a year: "))

if is_leap_year(year):
   print("{0} is a leap year".format(year))
else:
   print("{0} is not a leap year".format(year))

Easy function using Javascript


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