Given a date, return the corresponding day of the week for that date.
The input is given as three integers representing the day
, month
and year
respectively.
Return the answer as one of the following values { "Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "}
.
Example 1:
Input: day = 31, month = 8, year = 2019 Output: "Saturday "
Example 2:
Input: day = 18, month = 7, year = 1999 Output: "Sunday "
Example 3:
Input: day = 15, month = 8, year = 1993 Output: "Sunday "
Constraints:
1971
and 2100
.program main
implicit none
integer :: day, month, year
character(len=9) :: answer
call get_answer(day, month, year, answer)
print '(A)', trim(answer)
contains
subroutine get_answer(day, month, year, answer)
implicit none
integer, intent(in) :: day, month, year
character(len=9), intent(out) :: answer
select case (day + 1 + (month-1)*31 + (year-1971)*365)
case (0, 6)
answer = "Saturday "
case (1, 7)
answer = "Sunday "
case (2, 8)
answer = "Monday "
case (3, 9)
answer = "Tuesday "
case (4, 10)
answer = "Wednesday "
case (5, 11)
answer = "Thursday "
case default
answer = "Friday "
end select
end subroutine get_answer
end program main
Friday
! This program takes three integers as input representing the day, month, and year respectively.
! It returns the corresponding day of the week for that date.
program dayOfWeek
implicit none
integer :: day, month, year
character(len=9) :: dayOfWeek
! Read input
read(*,*) day, month, year
! Calculate day of the week
select case(mod(day + ((month - 1) * 30) + (year - 1971) * 365, 7))
case(0)
dayOfWeek = "Sunday "
case(1)
dayOfWeek = "Monday "
case(2)
dayOfWeek = "Tuesday "
case(3)
dayOfWeek = "Wednesday "
case(4)
dayOfWeek = "Thursday "
case(5)
dayOfWeek = "Friday "
case(6)
dayOfWeek = "Saturday "
end select
! Print output
write(*,*) dayOfWeek
end program dayOfWeek
temp.f95:8:33: 8 | character(len=9) :: dayOfWeek | 1 Error: Symbol ‘dayofweek’ at (1) cannot have a type temp.f95:16:22: 16 | dayOfWeek = "Sunday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:18:22: 18 | dayOfWeek = "Monday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:20:22: 20 | dayOfWeek = "Tuesday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:22:22: 22 | dayOfWeek = "Wednesday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:24:22: 24 | dayOfWeek = "Thursday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:26:22: 26 | dayOfWeek = "Friday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:28:22: 28 | dayOfWeek = "Saturday " | 1 Error: ‘dayofweek’ at (1) is not a variable temp.f95:32:24: 32 | write(*,*) dayOfWeek | 1 Error: Symbol at (1) is not appropriate for an expression
def day_of_the_week(day, month, year):
from datetime import date
days = ["Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "]
return days[date(year, month, day).weekday()]
The algorithm calculates the day of the week based on the given day, month, and year. For the C++ implementation, we implement the calculation of days since the year 1971 and then use the modulus operator to determine the day of the week. For Java, Python, and JavaScript implementations, we take advantage of built-in date libraries (LocalDate, datetime, and Date respectively) to calculate the day of the week. All implementations use a string array days[]
to get the correct day of the week as the output.
#include <string>
std::string dayOfTheWeek(int day, int month, int year) {
int days[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
std::string week[] = {"Sunday ", "Monday ", "Tuesday ", "Wednesday ", "Thursday ", "Friday ", "Saturday "};
if (year < 1971) year += (1971 - year) / 400 * 400;
int sum = -1;
for (int i = 1971; i < year; ++i) {
sum += 365;
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) ++sum;
}
for (int i = 1; i < month; ++i) {
sum += days[i];
if (i == 2 && year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ++sum;
}
return week[(sum + day) % 7];
}
The algorithm calculates the day of the week based on the given day, month, and year. For the C++ implementation, we implement the calculation of days since the year 1971 and then use the modulus operator to determine the day of the week. For Java, Python, and JavaScript implementations, we take advantage of built-in date libraries (LocalDate, datetime, and Date respectively) to calculate the day of the week. All implementations use a string array days[]
to get the correct day of the week as the output.