C Program To Display Month By Month For A Given Calendar Year

C Program To Display Month By Month For A Given Calendar Year

Ever wondered “How To Use C Program To Display Month By Month For A Given Calendar Year“? In this article, we will implement a simple yet powerful C program that generates a month for any year you choose. First, we will write code and then we will try to understand every function bit by bit.

Code To Generate Calendar For Any Year:

				
					#include <stdio.h>
#include <stdbool.h>
bool isLeapYear(int year) {
 return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
}
void displayCalendar(int year) {
 const char *months[] = {
 "January", "February", "March", "April",
 "May", "June", "July", "August",
 "September", "October", "November", "December"
 };
 int daysInMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
 if (isLeapYear(year)) {
 daysInMonth[1] = 29; // February has 29 days in a leap year
 }
 printf("\nCalendar for year %d:\n", year);
 for (int month = 0; month < 12; ++month) {
 printf("\n\t%s:\n", months[month]);
 printf(" Sun Mon Tue Wed Thu Fri Sat\n");
 // Calculate the starting day of the month (0: Sunday, 1: Monday, ..., 6: Saturday)
 int startingDay = year + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400;
 for (int i = 0; i < month; ++i) {
 startingDay += daysInMonth[i];
 }
 startingDay %= 7;
 // Print the empty cells for the starting day
 for (int i = 0; i < startingDay; ++i) {
 printf(" ");
 }
 // Print the days of the month
 for (int day = 1; day <= daysInMonth[month]; ++day) {
 printf("%4d ", day);
 if ((startingDay + day) % 7 == 0 || day == daysInMonth[month]) {
 printf("\n");
 }
 }
 }
}
int main() {
 int year;
 printf("Enter a year to display the calendar(e.g. 2023): ");
 scanf("%d", &year);
 if (year < 0) {
 printf("Invalid year!\n");
 return 1;
 }
 if (year < 100) {
 year += 2000; // Assume years less than 100 are in the 2000s
 }


 // Range where gregorian calendar can be shown without accuracies and overflows.
 if (year < 1582 || year > 4902) {
 printf("Invalid year!\n");
 return 1;
 }
 displayCalendar(year);
 return 0;
}
				
			

Faced difficulties in understanding the above code? No need to fret, you can take C programming help from Codingzap experts to clear all your doubts.

Output:

Enter a year to display the calendar(e.g. 2023): 2023

Calendar for 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

Let’s break down the code into simple steps:

Step 1:

Include Headers
The code starts by including two important header files: stdio.h for input and output functions, and
stdbool.h for using the bool data type and true/false values.

Step 2 :

Define `isLeapYear` function
This function helps to find out if a given year is a leap year. Leap years have an extra day (February
29). It does this by using the modulo operator % to check if the year is divisible by 4 and not divisible
by 100, or if it’s divisible by 400. If conditions are met, it return true or false.
According to the Gregorian calendar rules:
 Years divisible by 4 are potential leap years.
 Years divisible by 100 are not leap years unless they are also divisible by 400, in which case
they are leap years.

Step 3:

Define `displayCalendar` function
This function displays a calendar for the specified year by user. It initializes “months” and
“daysInMonth” for storing month names and the no of days in each month respectively.
If the year is a leap year, then no of days in February becomes 29.
The function shows the calendar header with the year. For each of the 12 months, it prints the month’s
name, day headers, calculates the starting day, aligns days, and handles line breaks.

Step 4:

Define main Function
– It prompts the user to enter a year and stores it in the `year` variable. It checks if the entered year is
negative. If so, it prints an error message and returns 1 to indicate an error.
– If the entered year is less than 100, it assumes that two-digit years refer to the 2000s and adjusts the
year accordingly.
– It checks if the entered year falls within a range (1582 to 4902) where the Gregorian calendar is valid.
If not, it prints an error message and returns 1.
– Finally, it calls the `displayCalendar` function to show the calendar for the specified year.

Leave a Comment

Your email address will not be published. Required fields are marked *