I've seen many questions similar to mine but haven't found anything that helps. I want to determine the day of the week of 1st given month. example: week day of March 01, 2021 or August 01, 2021. Only the month is asked from the user and it is given that Jan 01, 2021 is a Friday. This is my code so far:
#include <stdio.h>
char* MonthName(int m){
switch(m){
case 1:
return "January";
case 2:
return "February";
case 3:
return "March";
case 4:
return "April";
case 5:
return "May";
case 6:
return "June";
case 7:
return "July";
case 8:
return "August";
case 9:
return "September";
case 10:
return "October";
case 11:
return "November";
case 12:
return "December";
default:
return 0;
}
}
int MonthDays(int m){
switch(m){
case 1:
return (31);
case 2:
return (28);
case 3:
return (31);
case 4:
return (30);
case 5:
return (31);
case 6:
return (30);
case 7:
return (31);
case 8:
return (31);
case 9:
return (30);
case 10:
return (31);
case 11:
return (30);
case 12:
return (31);
}
}
char* WeekDay(){
int m, d, x;
x += 365;
x += MonthDays(m);
d = x%7;
switch(d){
//because the Jan 1 is a friday
case 0:
return "Friday";
case 1:
return "Saturday";
case 2:
return "Sunday";
case 3:
return "Monday";
case 4:
return "Tuesday";
case 5:
return "Wednesday";
case 6:
return "Thursday";
}
}
int main(){
int m;
char d;
printf("Choose a month number from 1-12: ");
scanf("%d", &m);
printf("The day of %s 01, 2021 is %s\n", MonthName(m), WeekDay(d));
}
I know my codes are a little long and messy, but I'm new to C programming and I don't want anything advanced since there aren't really any errors anymore. I'm hoping not to have to modify any of the codes/switches, just the formula because I really can't figure out how to get the day of the week (see function WeekDay(), specifically the one with variables x and d). It prints a week day but it's not the correct week day. I want to incorporate the MonthDays function, as well and most of the other answers on questions similar to mine doesn't use the number of days in a month.
Any help/advice would be appreciated. Thank you!