Download as pdf or txt
Download as pdf or txt
You are on page 1of 1

1: #include <stdio.

h>
2:
3: // Function to find the day of the week for a given date
4: int day_of_week(int d, int m, int y) {
5: static int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
6: y -= m < 3;
7: return (y + y / 4 - y / 100 + y / 400 + t[m - 1] + d) % 7;
8: }
9:
10: // Function to print the calendar of a given year
11: void printCalendar(int year) {
12: char *months[] = {
13: "January", "February", "March", "April", "May", "June",
14: "July", "August", "September", "October", "November", "December"
15: };
16: int daysInMonth[] = {
17: 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
18: };
19:
20: printf("Calendar for the year %d:\n", year);
21:
22: for (int month = 0; month < 12; month++) {
23: printf("\n---------------%s---------------\n", months[month]);
24: printf(" Sun Mon Tue Wed Thu Fri Sat\n");
25:
26: int startingDay = day_of_week(1, month + 1, year);
27: int daysInCurrentMonth = daysInMonth[month];
28:
29: if (month == 1 && (year % 4 == 0 && year % 100 != 0 || year % 400 == 0))
30: daysInCurrentMonth = 29;
31:
32: for (int i = 0; i < startingDay; i++)
33: printf(" ");
34: for (int day = 1; day <= daysInCurrentMonth; day++) {
35: printf("%4d", day);
36: if ((startingDay + day) % 7 == 0 || day == daysInCurrentMonth)
37: printf("\n");
38: }
39: }
40: }
41:
42: int main() {
43: int year;
44:
45: printf("Enter the year for the calendar: ");
46: scanf("%d", &year);
47:
48: printCalendar(year);
49:
50: return 0;
51: }
52:

You might also like