#include <stdio.h>
#include <time.h>
/*
This program finds the day of the week for January 1st of a given year.
Approach:
---------
We use the C standard library's <time.h> facilities, which include:
- struct tm : a calendar date/time structure
- mktime() : converts struct tm into a normalized time value and
fills in fields like tm_wday (day of week)
Why this is efficient:
----------------------
mktime() uses the system's optimized calendar routines, avoiding manual
arithmetic or algorithms. It is portable, reliable, and efficient.
tm_wday meaning:
0 = Sunday
1 = Monday
2 = Tuesday
3 = Wednesday
4 = Thursday
5 = Friday
6 = Saturday
*/
// Convert tm_wday (0..6) to a readable weekday name
const char* weekday_name(int wday) {
switch (wday) {
case 0: return "Sunday";
case 1: return "Monday";
case 2: return "Tuesday";
case 3: return "Wednesday";
case 4: return "Thursday";
case 5: return "Friday";
case 6: return "Saturday";
default: return "Unknown";
}
}
// Compute weekday of January 1st for a given year
const char* jan1_weekday(int year) {
struct tm date = {0};
date.tm_year = year - 1900; // years since 1900
date.tm_mon = 0; // January (0-based)
date.tm_mday = 1; // January 1st
// mktime normalizes the date and computes tm_wday
mktime(&date);
return weekday_name(date.tm_wday);
}
int main(void) {
int year = 2026;
const char* result = jan1_weekday(year);
printf("January 1st, %d falls on a %s.\n", year, result);
return 0;
}
/*
run:
January 1st, 2026 falls on a Thursday.
*/