Given the date in the form of dd-mm-yyyy, write a program to find the day of the year.
Answers
Answer:
# include <stdio.h>
# include <conio.h>
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() ;
}
Explanation:
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).