The Leap Year Function

My brain’s algorithm for determining whether the current year is a leap year is based on US Presidential elections. They always happen on a leap year. Or do they?

The Presidential Election of 2000 was a leap year, but the Presidential Election of 1900 was not. Fortunately, I wasn’t alive then. Nor will I be alive (most likely) in 2100, which is also a Presidential Election year, but not a leap year.

O leap year math!

Here are the leap year rules:

  • IF the year is evenly divisible by 4, it is a leap year . . .
  • UNLESS the year is also evenly divisible by 100, it’s not a leap year . . .
  • UNLESS it’s also divisible by 400, it is a leap year.

The year 1900 was evenly divisible by both 4 and 100, but not 400. It wasn’t a leap year.

The year 2000 was evenly divisible by both 4 and 100, so it shouldn’t be a leap year, but it’s also evenly divisible by 400. It was a leap year.

The Gregorian Calendar, introduced in 1582, uses these rules to keep dates aligned with the seasons and it’s proven to be quite effective since that time.

The first actual leap year under this new calendar occurred in 1584. Your task for this month’s Exercise is to displays values from that date until the year 2100, skipping every four years and proclaiming whether February in that specific year had a 29th day. Use the leap year rules stated above to craft a function that determines whether a given year is a leap year.

Try this Exercise on your own before you look at my solution.

Leave a Reply