In: Computer Science
C Question!
Problem: Given a day of the week (1-7 corresponding to Sunday through Saturday), the month number (1-12), and the year, determine whether that given month has five occurrences of the day of the week and display those dates.
Example Execution #1:
Enter day of week (1-7) -> 4
Enter month of the year -> 10
Enter the year -> 2019
Finding: There exists five Wednesday dates in October of 2019.
Dates: 2 9 16 23 30
Example Execution #2:
Enter day of week (1-7) -> 2
Enter month of the year -> 10
Enter the year -> 2019
Finding: There are not five Monday dates in October of 2019.
The C code is given below:
#include <stdio.h>
int main() {
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30,
31};
char* dayName[] = {"Sunday", "Monday", "Tuesday", "Wednesday",
"Thursday", "Friday", "Saturday"};
char* monthName[] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September", "October",
"November", "December"};
int dayOfWeek, month, year, i;
printf("Enter day of week (1-7) -> ");
scanf("%d", &dayOfWeek);
printf("Enter month of the year -> ");
scanf("%d", &month);
printf("Enter the year -> ");
scanf("%d", &year);
/* if the given year is leap then change the number of days of
February to 29. */
if (year % 4 == 0 && (year % 100 != 0 || year % 400 ==
0))
monthDays[1] = 29;
int date_of_month = 1; // we will find the day of 1st date of the
given month.
int days=1;
/* count total number of days from 1 january 1900 to 1st date of
the given month and year */
for (i = 1900; i < year; i++) // according to Gregorian calendar
1 january 1900 is Monday.
{
if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0)) // add
366 when year is leap
days += 366;
else // otherwise add 365
days += 365;
}
for (i = 0; i < month - 1; i++) // add number of days upto the
given month of the given year.
days += monthDays[i];
int weekDay = days%7 + 1; // find week day
while(weekDay != dayOfWeek) // find date of the first day of month
which is given
{
date_of_month++;
weekDay = weekDay%7 + 1;
}
int count = 0;
int temp = date_of_month;
while(date_of_month <= monthDays[month - 1]) // count number of
that day in the month.
{
count++;
date_of_month += 7;
}
if(count == 5)
{
printf("Finding: There exists five %s dates in %s of
%d.\n",dayName[dayOfWeek-1],monthName[month-1],year);
printf("Dates: ");
while(count--) // print all the 5 dates
{
printf("%d ",temp);
temp += 7;
}
}
else
printf("Finding: There are not five %s dates in %s of
%d.\n",dayName[dayOfWeek-1],monthName[month-1],year);
return 0;
}
Sample Input and Output 1:
Enter day of week (1-7) -> 4
Enter month of the year -> 10
Enter the year -> 2019
Finding: There exists five Wednesday dates in October of
2019.
Dates: 2 9 16 23 30
Sample Input and Output 2:
Enter day of week (1-7) -> 2
Enter month of the year -> 10
Enter the year -> 2019
Finding: There are not five Monday dates in October of 2019.
Screenshot of the code is given below:
If the answer helped please upvote, it means a lot and for any query please comment.