From 12c68a539b30ea366e25c94fd05bc83e90d575fe Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Fri, 6 Oct 2023 14:43:22 +0100 Subject: [PATCH] c10 --- C10/ex4.c | 42 ++++++++++++++++++++++++++++++++++++++++++ C10/ex5.c | 45 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 C10/ex4.c create mode 100644 C10/ex5.c diff --git a/C10/ex4.c b/C10/ex4.c new file mode 100644 index 0000000..f060508 --- /dev/null +++ b/C10/ex4.c @@ -0,0 +1,42 @@ +#include + +int date_to_day(int year, int month, int day) { + if (month < 3) { + month += 12; + year -= 1; + } + + return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7; +} + +int main() { + int rc, year, month, day, nd; + + printf("Enter a day (yyyy-mm-dd): "); + rc = scanf("%d-%d-%d", &year, &month, &day); + if (rc != 3) { + printf("Failed to parse date\n"); + return 1; + } + + nd = date_to_day(year, month, day); + + switch (nd) { + case 0: + printf("Monday\n"); break; + case 1: + printf("Tuesday\n"); break; + case 2: + printf("Wednesday\n"); break; + case 3: + printf("Thursday\n"); break; + case 4: + printf("Friday\n"); break; + case 5: + printf("Saturday\n"); break; + case 6: + printf("Sunday\n"); break; + } + + return 0; +} diff --git a/C10/ex5.c b/C10/ex5.c new file mode 100644 index 0000000..0899684 --- /dev/null +++ b/C10/ex5.c @@ -0,0 +1,45 @@ +#include + +int date_to_day(int year, int month, int day) { + if (month < 3) { + month += 12; + year -= 1; + } + + return ((13*month+3)/5 + day + year + year/4 - year/100 + year/400) % 7; +} + +void print_day_of_week(int day) { + switch (day) { + case 0: + printf("Monday\n"); break; + case 1: + printf("Tuesday\n"); break; + case 2: + printf("Wednesday\n"); break; + case 3: + printf("Thursday\n"); break; + case 4: + printf("Friday\n"); break; + case 5: + printf("Saturday\n"); break; + case 6: + printf("Sunday\n"); break; + } +} + +int main() { + int rc, year, month, day, nd; + + printf("Enter a day (yyyy-mm-dd): "); + rc = scanf("%d-%d-%d", &year, &month, &day); + if (rc != 3) { + printf("Failed to parse date\n"); + return 1; + } + + nd = date_to_day(year, month, day); + print_day_of_week(nd); + + return 0; +}