In this article, we will explore how to write a C program to display a month-by-month calendar for a given year. The program will calculate the number of days in each month and determine the day of the week for the first day of each month. Using this information, it will print a formatted calendar for each month, including the corresponding day of the week for each date.
Steps to Display a month-by-month calendar for a given year:
Step 1: Accept the input year from the user.
Step 2: Define functions to calculate the number of days in a month and the day of the week for the first day of a given month.
Step 3: Iterate through each month of the year and generate the calendar for each month.
Step 4: Format and display the calendar for each month, including the month name and the corresponding day of the week for each date.
Step 5: Repeat the above steps for all twelve months of the year.
Code Example:
//C Program to print month by month calendar for given year #include <stdio.h> //function to check leap year int isLeapYear(int year) { if (year % 400 == 0) return 1; if (year % 100 == 0) return 0; if (year % 4 == 0) return 1; return 0; } //function to get the number of day in that month int getNumberOfDays(int month, int year) { int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}; if (month == 2 && isLeapYear(year)) return 29; else return daysInMonth[month - 1]; } // int getDayOfWeek(int day, int month, int year) { int t[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4}; if (month < 3) year--; int dayOfWeek = (year + year / 4 - year / 100 + year / 400 + t[month - 1] + day) % 7; return dayOfWeek; } void displayCalendar(int year) { char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; for (int month = 1; month <= 12; month++) { int days = getNumberOfDays(month, year); int firstDayOfWeek = getDayOfWeek(1, month, year); printf("\n--------------- %s ---------------\n", months[month - 1]); printf("Sun Mon Tue Wed Thu Fri Sat\n"); int day; for (int space = 0; space < firstDayOfWeek; space++) printf(" ");
for (day = 1; day <= days; day++) { printf("%3d ", day); if ((day + firstDayOfWeek) % 7 == 0) printf("\n"); }
if ((day + firstDayOfWeek) % 7 != 0) printf("\n"); } } int main() { int year; printf("Enter the year: "); scanf("%d", &year); displayCalendar(year); return 0; }
Enter the year: 2023
--------------- January ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- February ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- March ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- April ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- May ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- June ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- July ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- August ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- September ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- October ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- November ---------------
Sun Mon Tue Wed Thu Fri Sat
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
--------------- December ---------------
Sun Mon Tue Wed Thu Fri Sat
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
- Time Complexity: The time complexity of the code can be considered as O(1) since the majority of the operations involve constant time complexity.
- Space Complexity: The space complexity of the code is constant or O(1).
No comments:
Post a Comment