(Display leap years) Write a program that displays, ten per line, all the leap years in the twenty-first century (from year 2001 to 2100). The years are separated by exactly one space.
Display leap years Program code:
# Define the starting and ending years
startYear = 2001
endYear = 2100
# Initialize a counter for leap years
leapYearCount = 0
# Iterate over the years in the range
for year in range(startYear, endYear + 1):
# Check if the year is a leap year
if (year % 4 == 0 and year % 100 != 0) or year % 400 == 0:
# Display the leap year
print(year, end=" ")
# Increment the leap year counter
leapYearCount += 1
# Check if we have printed ten leap years
if leapYearCount % 10 == 0:
# Start a new line
print()
# Print a new line if the last line doesn't contain 10 years
if leapYearCount % 10 != 0:
print()
Executed Output:
2004 2008 2012 2016 2020 2024 2028 2032 2036 2040
2044 2048 2052 2056 2060 2064 2068 2072 2076 2080
2084 2088 2092 2096