Code to get day of given date in c language
#include <stdio.h>
#include<string.h>
int main()
{
int month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
char week[7][10] ;
int date, mon, year, i, r, s = 0 ;
strcpy(week[0], "Saturday") ;
strcpy(week[1], "Monday") ;
strcpy(week[2], "Tuesday") ;
strcpy(week[3], "Wednesday") ;
strcpy(week[4], "Thursday") ;
strcpy(week[5], "Friday") ;
strcpy(week[6], "Sunday") ;
printf("Date format (dd/mm/yyyy) : ") ;
scanf("%d / %d / %d", &date, &mon, &year) ;
if( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) )
month[1] = 29 ;
for(i = 0 ; i < mon - 1 ; i++)
s = s + month[i] ;
s = s + (date + year + (year / 4) - 2) ;
s = s % 7 ;
printf("\nDay =%s", week[s]) ;
return 0;
}
Algorithm
1. Input Date in format : dd/mm/yyyy
2.check if leap year or not
3.if yes
then add February month to 29 days
else
set it to 28 days which is default set.
4.for each month calculate the value of s,
s = s + month[i] ;
s = s + (date + year + (year / 4) - 2) ;
s = s % 7
5. print the Day of given date.