-
Notifications
You must be signed in to change notification settings - Fork 0
/
8.1.c
65 lines (51 loc) · 1.67 KB
/
8.1.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
int main() {
int year, firstDay;
printf("Enter the year: ");
scanf("%d", &year);
printf("Enter the first day of the year (0=Sunday, 1=Monday, ..., 6=Saturday): ");
scanf("%d", &firstDay);
printf("\nCalendar for the year %d:\n\n", year);
int month;
int currentDay = firstDay;
for (month = 1; month <= 12; month++) {
printf("Month %d:\n", month);
printf("Sun Mon Tue Wed Thu Fri Sat\n");
// Print leading spaces for the first week
for (int i = 0; i < currentDay; i++) {
printf(" ");
}
int maxDays;
// Calculate the number of days in the current month
switch (month) {
case 2:
// February: Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
maxDays = 29;
} else {
maxDays = 28;
}
break;
case 4:
case 6:
case 9:
case 11:
maxDays = 30;
break;
default:
maxDays = 31;
}
// Print the days of the month
for (int day = 1; day <= maxDays; day++) {
printf("%3d ", day);
// If it's the last day of the week, move to the next line
if ((currentDay + day) % 7 == 0) {
printf("\n");
}
}
printf("\n");
// Calculate the first day of the next month
currentDay = (currentDay + maxDays) % 7;
}
return 0;
}