given a year, determine whether it is a leap year. if it is a leap year, return the boolean true, otherwise return false. note that the code stub provided reads from stdin and passes arguments to the is_leap function. it is only necessary to complete the is_leap function.
What is a leap year and how to determine if a given year is a leap year?
A leap year is a year that contains an extra day, February 29th, making it 366 days instead of the usual 365. Leap years are necessary to keep our calendar in alignment with the Earth’s revolutions around the Sun. However, not every year divisible by 4 is a leap year. There are additional rules to determine leap years:
- A year is a leap year if it is divisible by 4, but not divisible by 100, except if it is also divisible by 400.
To determine if a given year is a leap year in the provided code stub, we need to complete the is_leap
function. This function should take a year as input and return True
if it is a leap year, or False
if it is not.
Here’s an example solution for the is_leap
function in Python:
def is_leap(year):
if year % 4 == 0:
if year % 100 == 0:
if year % 400 == 0:
return True
else:
return False
else:
return True
else:
return False
In this solution, we first check if the year is divisible by 4. If it is, we proceed to check if it is divisible by 100. If it is, we further check if it is divisible by 400. If all these conditions are met, we return True
as it is a leap year. Otherwise, we return False
.
Make sure to integrate this is_leap
function into the provided code stub to complete the solution.