Question

In: Computer Science

Day of week Write a program that asks the user for a date (year, then month,...

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

Solutions

Expert Solution

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


Related Solutions

Write a c++ program that asks the user for a date (Month and Day only) and...
Write a c++ program that asks the user for a date (Month and Day only) and displays the season in this day.The program asks then the user if he needs to enter another date, if he answers with ’Y’ or ’y’, the programwill take another date from the user and displays the season in this day.The program will keep repeating this until the user enters a character different than ’Y’ and ’y’.•Fall starts in September 21st•Winter starts in December 21st•Spring...
In a file called NumbersToMonths.java, write a program that: Asks the user to specify a month...
In a file called NumbersToMonths.java, write a program that: Asks the user to specify a month as an integer between 1 and 12. It is OK for your program to crash when the user does not enter a valid integer. Prints out the name of the corresponding month. Prints out "This number does not correspond to a month." if the integer is less than 1 or greater than 12. For example: if the user enters 1, your program output should...
Write a program using switch statement that asks user to enter the month number and then...
Write a program using switch statement that asks user to enter the month number and then it prints out the number of days for that month. For simplicity reasons have your program print 28 days for February no matter if it is a leap year or not. Your program should also handle any invalid month numbers that user could enter (hint use default for the switch). Use a while loop to allow user to test for different month entries till...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year:...
Date - month: int - day: int - year: int +Date() +Date(month: int, day: int, year: int) +setDate(month: int, day: int, year: int): void -setDay(day: int): void -setMonth(month: int): void -setYear(year: int): void +getMonth():int +getDay():int +getYear():int +isLeapYear(): boolean +determineSeason(): string +printDate():void Create the class Constructor with no arguments sets the date to be January 1, 1900 Constructor with arguments CALLS THE SET FUNCTIONS to set the Month, then set the Year, and then set the Day - IN THAT ORDER...
Write a query to return the date and day of the week of the first day...
Write a query to return the date and day of the week of the first day of the month two years from today. If today is 10/16/20, then the expect output is 10/01/2022 and the day of the week is thursday. I am using postgresql.
Write a program named CheckMonth2 that prompts a user to enter a birth month and day....
Write a program named CheckMonth2 that prompts a user to enter a birth month and day. Display an error message that says Invalid date if the month is invalid (not 1 through 12) or the day is invalid for the month (for example, not between 1 and 31 for January or between 1 and 29 for February). If the month and day are valid, display them with a message. For example, if the month entered is 2, and the day...
Write a program that asks the user for an integer. The program checks and prints to...
Write a program that asks the user for an integer. The program checks and prints to the screen whether the number is prime or not. For example, if user enters 17, the program should print “17 is prime”; if the user enters 20, the program should print “20 is not prime”. please do it with a “ while Loop”, Thanks..
Python: Write a program that asks the user for the name of a file. The program...
Python: Write a program that asks the user for the name of a file. The program should display the contents of the file line by line.
Write a program that asks the user to enter the name of a file, and then...
Write a program that asks the user to enter the name of a file, and then asks the user to enter a character. The program should count and display the number of times that the specified character appears in the file. Use Notepad or another text editor to create a sample file that can be used to test the program. Sample Run java FileLetterCounter Enter file name: wc4↵ Enter character to count: 0↵ The character '0' appears in the file...
Write a program that asks the user for the lengths of the sides of a rectangle....
Write a program that asks the user for the lengths of the sides of a rectangle. Again, check for valid input and exit with an error msg if you don’t get it. Testing: use some known values to confirm that the calculations are correct. E.g. 3 – 4 - 5 triangle >> 3 X 4 rectangle Then print • The area and perimeter of the rectangle • The length of the diagonal (use the Pythagorean theorem). This question should be...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT