Ever wondered “How To Use the C Program To Display Months 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.
Calendars can be a useful tool in software. Whether you are creating a goal-tracker tool or a schedule-tracking system, displaying a calendar is one of the basic steps. So, this is what we are going to do in this article.
So, do you want to know about handling dates and calendar tasks? Let us get started with today’s topic!
Summary Of The Article:
- A calendar is a useful tool that is used by everyone for their respective purpose.
- It is helpful in managing tasks, scheduling appointments, keeping track of your productivity etc.
- As a software engineer, you should know how to program this in the programming language of your choice.
- Adding a calendar to your software project related to tracking daily activities is a great way to include facilities for time management and task prioritization.
How To Code A C Program To Display Complete Calendar (Example)?
Let us know how we can start with creating a calendar in the C programming language. To understand how it will work, I have added a flowchart for the same. It will help you get a proper idea of the calendar program. Take a look at it in the image below.
Here, we need to make an internationally accepted calendar like the Gregorian calendar. Therefore, we will need to add a few lines to check if the user inputs are meeting all the conditions or not.
As shown in the flowchart, the main function will be executed first which will prompt for the user input. We will do input checking here to see if the entered year is valid or not. After this, we will call the function that we have created to display the calendar.
Let’s see the implementation and explanation of the program below. First, we will write code and then we will try to understand every function bit by bit.
#include
#include
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.
If the code is not written well, it might flag an error in the C programming project.
Output:
Enter a year to display the calendar(e.g. 2024): 2024
Calendar for the year 2024:
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 29
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). Strictly speaking, we will have to not just see the divisibility by 4 but also by 400.
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
The next function is an important one! This is the function where we pass the input (int year) as the parameter. You can make a separate function calendar year for taking the input if you want.
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 weย must add one more day in one month. So, February will have 29 days for this case.
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.
Here, we have made two arrays, one stores the number of days in one month, and the other stores the name of the months. We need to output the right weekday at the correct position for all the dates.
Step 4:
Define the 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.
This is how we can create our program for displaying all the days in the calendar.
Conclusion:
So, this is how you can print or display month-by-month for a calendar year in C programming. I hope that you have understood the code and the functions in it with the help of the explanation provided.
The calendar application has a lot of use in other software applications as well. It is a basic tool that you can include in your projects to make them a little more elaborate. For more such programs on programming, check out our other articles as well!
Takeaways:
- Displaying a calendar year is one of the basic programs that you can practice during your day-to-day code practice.
- C programming is a fundamental coding language that every software developer should know about. And performing tasks related to date, time, and calendars helps to grasp the concepts.
- While creating your calendar function, make sure you perform error handling and exception handling as well, so that your program doesn’t encounter bugs.ย