Program to Find the Day of the Given Date in C | C Program

Here’s a C program to find the day of the given date with output and proper explanation. The program takes as input a formatted date and returns the day on that date. This program makes use of single and multidimensional arrays, for loop and string function - strcpy() (string copy).


# include  
# include  
void 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 ; 
 clrscr(); 
 strcpy(week[0], "Sunday") ; 
 strcpy(week[1], "Monday") ; 
 strcpy(week[2], "Tuesday") ; 
 strcpy(week[3], "Wednesday") ; 
 strcpy(week[4], "Thursday") ; 
 strcpy(week[5], "Friday") ; 
 strcpy(week[6], "Saturday") ; 
 printf("Enter a valid date (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("\nThe day is : %s", week[s]) ; 
 getch() ; 
}

Output of above program is



Enter a valid date (dd/mm/yyyy) : 02/11/1977

The day is: Wednesday

Comments