In: Computer Science
Day of week
Write a program that asks the user for a date (year, then month, then day, each entered as a number on a separate line), then calculates the day of the week that date falls on (according to the Gregorian calendar) and displays the day name.
You may not use any date functions built into Python or its standard library, or any other functions that do this work for you.
Hint: Reverend Zeller may be useful, but his years start in a weird spot.
Give credit to your sources by including an exact, working URL in a comment at the top of your program.
LAB
Here is the completed code for this problem. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks
#code
'''
In this program, we are using Zeller's congruence to find the day
of week
According to the algorithm, day of week can be calculated using
below equation:
day_of_week=[day_of_month + 13(month+1)//5 + K + K//4 + J//4 -2J ]
% 7
where // represents integer division, K represents year of the
century,
J represents zero based century (year//100)
Notes: according to this algorithm day starts with saturday, and
month number
for january is 13 and february is 14 (starts with march = 3)
For reference, visit Zeller's congruence wikipedia page
'''
#creating a list containing day names
days=["Saturday","Sunday","Monday","Tuesday","Wednesday","Thursday","Friday"]
#note: assuming all user inputs are valid
#getting year
year=int(input("Enter a year: "))
#getting month (1-12)
month=int(input("Enter a month (1-12): "))
#setting month to 13 or 14 if the month is January or
February
if month==1 or month==2:
month=12+month
#reading day
day=int(input("Enter a day: "))
#applying the equation to get day of week (0-6)
day_of_week=(day+(13*(month+1)//5)+(year%100)+((year%100)//4)+((year//100)//4)-(2*(year//100)))%7
#displaying day at index day_of_week in days list
print("Day of the week is",days[day_of_week])
#output
Enter a year: 2020
Enter a month (1-12): 4
Enter a day: 17
Day of the week is Friday