In this program, we will take a range of leap years as input and find the number of leap years within a range of years.
Steps to solve the program
- Take a range of years as input.
- Split the range using split() so that will get a list of starting year and ending year.
- Create a variable and assign its value equal to 0.
- Use for loop to get a range of years between starting year and the ending year.
- During iteration check whether a year is a leap year to not.
- If it is a leap year then add 1 to the variable that we created at the start.
- Print the output.
#Input string
str1 ="1981-2001"
l = str1.split("-")
total = 0
for i in range(int(l[0]),int(l[1])+1):
if (i % 400 == 0 or i % 100 !=0) and i % 4 == 0:
total += 1
#Printing output
print("Range of years:", str1)
print("Total leap year: ",total)
Output :
Range of years: 1981-2001
Total leap year: 5