In: Computer Science
Use Zeller’s algorithm to figure out the days of the week for a given date in c.
W = (d + 13(m+1)/5 + y + y/4 + c/4 - 2c + 6) mod 7
Y is the year minus 1 for January or February, and the year for any other month
y is the last 2 digits of Y
c is the first 2 digits of Y
d is the day of the month (1 to 31)
m is the shifted month (March=3,...January = 13, February=14)
w is the day of week (0=Sunday,..,6=Saturday)
Output should look the following:
Please enter the day [YYYY/Month/Day]: 2020/1/1
Wednesday
I have written the program using C PROGRAMMING LANGUAGE.
INPUT:
I have given the Input date as 24/09/2019 (DD/MM/YYYY)
OUTPUT :
CODE :
//included the stdio library
#include <stdio.h>
//function definition for getFirstDayOfMonth
int getFirstDayOfMonth(int mon, int year) {
//Zeller's algorithm
int Adjustedmonth,Adjustedyear,day,century,output;
//declare adjusted month, set to (mon + 9) % 12 + 1
Adjustedmonth = (mon + 9) % 12 + 1;
//declare adjusted year, set to year - 2000
Adjustedyear = year - 2000;
/*if adjusted month is greater than 10:
subtract one from adjusted year*/
if(Adjustedmonth > 10){
Adjustedyear = Adjustedyear - 1;
}
//declare day, set to 1
day = 24;
//declare century, set to 20
century = 20;
/*declare output, set to (13 * adjusted mon - 1) / 5 + adjusted year / 4 + century / 4 + day + adjusted year - 2 * century;*/
output = (13 * Adjustedmonth - 1) / 5 + Adjustedyear / 4 + century / 4 + day + Adjustedyear - 2 * century;
// mod output by 7
output = output%7;
//add 7 to output
output = output + 7;
//mod output by 7 again
output = (output+1)%7;
return output; //returned the output
}
//main method
int main(void) {
int result ;//declared the result integer variable
//Calling the getFirstDayOfMonth function
result = getFirstDayOfMonth(9,2019);
printf("\nOUTPUT : \n");//print statement
//Switch Case
switch (result)
{
case 0 : printf("Saturday \n"); break;
case 1 : printf("Sunday \n"); break;
case 2 : printf("Monday \n"); break;
case 3 : printf("Tuesday \n"); break;
case 4 : printf("Wednesday \n"); break;
case 5 : printf("Thursday \n"); break;
case 6 : printf("Friday \n"); break;
}
return 0;//returned 0
}
Thanks..