#include <stdio.h>
#include <string.h>
#define NUM_DAYS 7
// Function to determine the nth occurrence of a specific day in a leap year
int nth_day_of_week(char *day_name, int nth) {
char *days[NUM_DAYS] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
// Find the index of the requested day
int day_index = -1;
for (int i = 0; i < NUM_DAYS; i++) {
if (strcmp(day_name, days) == 0) {
day_index = i;
break;
}
}
if (day_index == -1 || nth <= 0) {
return 0; // Invalid day name or nth value
}
// Calculate the nth occurrence
int occurrence = 0;
for (int day_of_year = 1; day_of_year <= 366; day_of_year++) {
// Check if the current day is the target day of the week
if ((day_of_year + 1) % NUM_DAYS == (day_index + 1) % NUM_DAYS) { // 1 Jan 2024 is a Monday
occurrence++;
if (occurrence == nth) {
return day_of_year; // Return the day of the year
}
}
}
return 0; // Not enough occurrences
}
int main() {
char day_name[] = "Wednesday";
int nth = 2; // Change this to find the 1st, 2nd, etc. occurrence
int day_of_year = nth_day_of_week(day_name, nth);
if (day_of_year > 0) {
printf("The %d occurrence of %s in 2024 is on day %d of the year.\n", nth, day_name, day_of_year);
} else {
printf("Invalid input or not enough occurrences of %s.\n", day_name);
}
return 0;
}