C program to display calendar for a given year
Tags: c, challenge, computerscience
Published: 2023-03-14 12:01:03

A calendar is an essential tool that is used by people all over the world to manage their schedules and appointments. It helps in planning events, meetings, and tasks, and keeps track of important dates and holidays. In this article, we will learn how to write a C program to display a calendar for a given year. We will use the Gregorian calendar, which is the most widely used calendar system in the world.

## Code
We will begin by defining the main function, which will take the year as input and display the calendar for that year. We will use a two-dimensional array to represent the calendar, with each row representing a week and each column representing a day. The program will use a loop to fill in the array with the dates for each day of the year.

```c
#include <stdio.h>

int main()
{
    int year, month, day;
    int daysInMonth, startingDay;

    printf("Enter the year: ");
    scanf("%d", &year);

    char *months[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    int daysInWeek = 7;
    int calendar[6][7];

    // Calculate starting day
    startingDay = getStartingDay(year);

    // Fill in the calendar with dates
    int row = 0;
    for (month = 0; month < 12; month++)
    {
        daysInMonth = getDaysInMonth(month, year);

        // Print month and year header
        printf("\n     %s %d\n", months[month], year);
        printf("Sun Mon Tue Wed Thu Fri Sat\n");

        // Fill in the days for the month
        for (day = 1; day <= daysInMonth; day++)
        {
            calendar[row][startingDay] = day;
            startingDay++;

            // Move to the next row if necessary
            if (startingDay > 6)
            {
                startingDay = 0;
                row++;
            }
        }

        // Print the calendar for the month
        for (int i = 0; i < 6; i++)
        {
            for (int j = 0; j < 7; j++)
            {
                if (calendar[i][j] == 0)
                {
                    printf("    ");
                }
                else
                {
                    printf("%3d ", calendar[i][j]);
                }
            }
            printf("\n");
        }

        // Reset the row and starting day for the next month
        row = 0;
        startingDay = (startingDay + daysInMonth) % daysInWeek;
    }
}
```

We will define two helper functions to calculate the starting day of the year and the number of days in each month. The getStartingDay function will take the year as input and return the starting day of the year, which will be a number between 0 and 6 representing the day of the week. The getDaysInMonth function will take the month and year as input and return the number of days in that month.

```c
int getStartingDay(int year)
{
    int d = (1 + (year - 1) * 365 + (year - 1) / 4 - (year - 1) / 100 + (year - 1) / 400) % 7;
    return d;
}

int getDaysInMonth(int month, int year)
{
    int daysInMonth;

    if (month == 1)
    {
        if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
        {
            daysInMonth = 29;
        }
        else
        {
            daysInMonth = 28;
        }
    }
    else if (month == 3 || month == 5 || month == 8 || month == 10)
    {
        daysInMonth = 30;
    }
    else
    {
        daysInMonth = 31;
    }

    return daysInMonth;
}
```

The getStartingDay function uses a formula to calculate the starting day of the year based on the year number. The formula takes into account the number of days in previous years, as well as the number of leap years that occurred in that time. The function returns the starting day as a number between 0 and 6, with 0 representing Sunday.

The getDaysInMonth function takes the month and year as input and uses a conditional statement to determine the number of days in that month. February has a different number of days in leap years than in normal years, so we check if the year is a leap year before setting the number of days in February. The function returns the number of days in the specified month.

Finally, we will create an array of month names that will be used to display the month and year header for each month. We will also define the number of days in a week, which is 7, and the calendar array, which will hold the dates for each day of the year.

The program will use a loop to iterate through each month of the year, calling the helper functions to get the starting day and number of days in each month. It will then fill in the calendar array with the dates for each day of the month, and display the calendar for that month using a nested loop.

You’ll see the following output once you run this above code:

![c program to display calendar for a given year](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rr9917onqrii5m1nudm8.jpg)

_Originally published at [https://programmingeeksclub.com/c-program-to-display-calendar-for-a-given-year/](https://programmingeeksclub.com)_

_You can follow me on [Twitter](https://twitter.com/kusinghofficial), [Facebook](https://www.facebook.com/profile.php?id=100086258693659), [Telegram](https://t.me/dpgcl), [YouTube](https://www.youtube.com/channel/UCTpm2yuM55e09RL3WLA0bFg), [My Blog](https://programmingeeksclub.com/)._