Table of contents
1.
Introduction
2.
C Program to Display Month-by-Month Calendar for a Given Year
2.1.
Steps to Create the Calendar
2.2.
Implementation
2.3.
Time and Space Complexity
2.3.1.
Time Complexity
2.3.2.
Space Complexity
3.
Frequently Asked Questions
3.1.
How does the program determine which day the year starts on?
3.2.
Can this program handle leap years?
3.3.
What happens if I enter a negative year?
4.
Conclusion
Last Updated: Mar 3, 2025
Medium

Calendar Program in C

Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

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. 

Calendar Program in C

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

  1. Take a year as input.
     
  2. Determine if it is a leap year.
     
  3. Calculate the starting day of the year.
     
  4. 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;
}
You can also try this code with Online C Compiler
Run Code

 

Explanation of Code

  1. Leap Year Calculation: The isLeapYear function checks if a year is a leap year.
     
  2. Starting Day Calculation: getFirstDayOfYear determines the first day of the year using Zeller’s formula.
     
  3. Calendar Printing: The printCalendar function prints each month with correct alignment.
     
  4. Looping for Months and Days: The program iterates through each month and prints the dates in a weekly format.

Example Output

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 ComplexityO(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).

Frequently Asked Questions

How does the program determine which day the year starts on?

 The program uses Zeller’s formula to find the starting weekday of the given year.

Can this program handle leap years?

Yes, the program adjusts February’s days based on leap year calculations.

What happens if I enter a negative year?

 The program does not restrict input, but it may not work correctly for negative years.

Conclusion

In this article, we learned how to create a Calendar Program in C, which displays months and dates based on user input. The program uses loops, functions, and conditional statements to generate a structured calendar. It helps in understanding date handling, formatting, and logic building in C. Learning to create a calendar program enhances problem-solving and coding skills.

Live masterclass