Skip to content

Instantly share code, notes, and snippets.

@bharathyes
Created June 27, 2015 07:04
Show Gist options
  • Save bharathyes/b9b0f90c71ddc78d02a7 to your computer and use it in GitHub Desktop.
Save bharathyes/b9b0f90c71ddc78d02a7 to your computer and use it in GitHub Desktop.
Find the day of a given date -- C program
#include <stdio.h>
int main () {
int d=27, m=6, y=2015, weekday ;
printf("Enter date, month and year: ");
scanf("%d%d%d",&d,&m,&y);
weekday = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 ;
printf("The day of the week is : %d",weekday);
switch(weekday) {
case 0:
printf(" Sunday.\n");
break;
case 1:
printf(" Monday.\n");
break;
case 2:
printf(" Tuesday.\n");
break;
case 3:
printf(" Wednesday.\n");
break;
case 4:
printf(" Thursday.\n");
break;
case 5:
printf(" Friday.\n");
break;
case 6:
printf(" Saturday.\n");
}
return 0;
}
@tarun6196
Copy link

I was unable to understand the logic for the weekday can any one help me please.

@buela25
Copy link

buela25 commented Sep 1, 2018

I too face the same problem.
Can you explain it clearly?

@dukuo
Copy link

dukuo commented Sep 2, 2019

For what I determined this could be a variant or simplification of Zeller's congruence but I'm either too dumb or I don't have time at the moment to understand the modifications. It works tho, so that's good.
Also, the line weekday = (d+=m<3?y--:y-2,23*m/9+d+4+y/4-y/100+y/400)%7 ; could be expanded as

d+=m<3?y--:y-2;
weekday  = Math.floor((23*m/9+d+4+y/4-y/100+y/400)%7 );

to port this code to javascript.

@tarun6196 @buela25

@R0b3rt020
Copy link

thank you, works fine

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment