In: Computer Science
Create a python program to determine which years are leap years (see key information at the bottom). Note: Do not use any lists.
1. Define a function that takes a parameter (year) and verifies if the parameter year is a leap year. If it is a leap year, the function returns True. Else, it returns False. Note: the function should not print anything.
2. Using the function, check if each of the years from 2000 to 2200 (both inclusive) is a leap year.
3. Output all the leap years between 2000 and 2200 in a single line
4. Program should end by printing the number of leap years found, with a short message.
Key information to determine if a given year is a leap year or not:
1. Determine whether the year is divisible by 100. If it is, then the year is a leap year if and only if the year is also divisible by 400. For example, 2000 is a leap year, but 2100 is not.
2. If the year is NOT divisible by 100, then the year is a leap year if and only if it is divisible by four. For example, 2008 is a leap year, but 2009 is not
Python code:
#defining function to print leap year
def leap_year(year):
#checking if the year is divisible by 4
if(year%4==0):
#checking if the year is divisible by 100
if(year%100==0):
#checking if the year is divisible by 400
if(year%400==0):
#returning True
return True
else:
#returning False
return False
else:
#returning True
return True
else:
#returning False
return False
#count to keep track of number of leap years
count=0
#looping from 2000 to 2201
for i in range(2000,2201):
#checking if the year is leap year
if(leap_year(i)):
#incrementing count
count+=1
#printing year
print(i,end=" ")
#printing Number of leap years found
print("\nNumber of leap years found:",count)
Screenshot:
Output: