Introduction
A Calendar Program in C allows users to view the calendar for a given month or year. It typically calculates the day of the week for the first day of the month and displays dates accordingly. The program uses loops, conditions, and date functions to format the output.

In this article, you will learn how to create a calendar program in C, its implementation, and an example to display a monthly or yearly calendar.
C Program to Display Month-by-Month Calendar for a Given Year
To create a calendar program in C, we need to consider leap years, days in a month, and how to determine the first day of the year.
Steps to Create the Calendar
- Take a year as input.
- Determine if it is a leap year.
- Calculate the starting day of the year.
- Print each month with proper spacing.
Implementation
Here is a C program that displays a calendar for a given year:
#include <stdio.h>
// Function to check if a year is a leap year
int isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return 1;
}
return 0;
}
// Function to get the first day of the year
int getFirstDayOfYear(int year) {
int day = (year + (year - 1)/4 - (year - 1)/100 + (year - 1)/400) % 7;
return day;
}
// Function to print the calendar
void printCalendar(int year) {
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};
int startDay = getFirstDayOfYear(year);
if (isLeapYear(year)) {
daysInMonth[1] = 29;
}
for (int i = 0; i < 12; i++) {
printf("\n\n --------- %s %d ---------\n", months[i], year);
printf(" Sun Mon Tue Wed Thu Fri Sat\n");
for (int k = 0; k < startDay; k++) {
printf(" ");
}
for (int j = 1; j <= daysInMonth[i]; j++) {
printf("%5d", j);
if ((j + startDay) % 7 == 0) {
printf("\n");
}
}
startDay = (startDay + daysInMonth[i]) % 7;
}
}
int main() {
int year;
printf("Enter the year: ");
scanf("%d", &year);
printCalendar(year);
return 0;
}
Explanation of Code
- Leap Year Calculation: The isLeapYear function checks if a year is a leap year.
- Starting Day Calculation: getFirstDayOfYear determines the first day of the year using Zeller’s formula.
- Calendar Printing: The printCalendar function prints each month with correct alignment.
- Looping for Months and Days: The program iterates through each month and prints the dates in a weekly format.
Example Output

Time and Space Complexity
Time Complexity
- Checking if a year is a leap year: O(1)
- Determining the first day of the year: O(1)
- Printing the calendar: O(12 * 31) ≈ O(1) (since we iterate through each month)
- Total Time Complexity: O(1) (constant time)
Space Complexity
- The program uses a few integer variables and two arrays (months and daysInMonth): O(1).
- Since no additional space is allocated dynamically, the overall space complexity is O(1).