In: Computer Science
Design and Write a program that asks for a 4-digit year and determines whether that year is a leap year or not.
This should work for any year from 1700 to 2022. Anything not completely obvious to a newbie must use # notes on lines to explain. Must be modularized and repeatable. Should have an invocation of the main routine at the end of the program.
Must have Flowgorithm and Python code.
Answer
Here is your answer, any doubt please comment,
here is the code for the above problem, finding the given year is leap year or not.
def leapYear(year):
"""
A leap year is exactly divisible by 4 except for year end with 00.
The century year is a leap year only if it is perfectly divisible by 400
"""
if (year % 4) == 0: #%is used to find the reminder
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year)) #format the string.
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
def main():
"""
create a loop for ask user input and check wheather it lies in between 1700 and 2020, then check it is leap year or not
"""
while 1:
year = int(input("Enter a year(quit enter 1): ")) #ask user for input
if year==1: #if user enter 1 ith will break and stop the program
break
if year>=1700 and year<=2020:
leapYear(year)
else:
print("Check the year in between 1700 - 2020")
if __name__=="__main__":
main() # invocation of the main routine at the end of the program.
i put comment in the above code please look into that, any doubt in the code please comment.
output
Thanks in advance