c-program-to-display-calendar-for-a-given-year

C program to display calendar for a given year

Home » Programming Language » C Language » C program to display calendar for a given year
c-program-to-display-calendar-for-a-given-year
c program to display calendar for a given year – programmingeeksclub

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.

Prerequisites

Before we start writing the program, we need to understand some basic concepts related to calendars. The Gregorian calendar system is a solar calendar that is based on the Earth’s revolution around the sun. It consists of 365 days in a year, with an additional day added in leap years to account for the extra time it takes for the Earth to complete one revolution. Leap years occur every four years, except for years that are divisible by 100 but not by 400.

The first day of the year is usually January 1st, which is considered a public holiday in most countries. The days of the week are numbered from 0 to 6, with 0 representing Sunday and 6 representing Saturday. In addition, each month has a different number of days, with February having 28 days in a normal year and 29 days in a leap year.

Now that we have a basic understanding of the Gregorian calendar system, we can proceed with writing the program.

Program

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.

#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.

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
C program to display calendar for a given year output – programmingeeksclub

Full Code

Conclusion

In this article, we have learned how to write a C program to display a calendar for a given year using the Gregorian calendar system. We have defined the main function, as well as two helper functions to calculate the starting day of the year and the number of days in each month. We have also used a two-dimensional array to represent the calendar, and filled it in with the dates for each day of the year. The program uses a loop to iterate through each month of the year, and displays the calendar for that month using a nested loop. This program can be useful for anyone who needs to manage their schedules and appointments, and wants a convenient way to view the calendar for a given year.

your comments are appreciated and if you wants to see your articles on this platform then please shoot a mail at this address kusingh@programmingeeksclub.com

Thanks for reading 🙂

Join Our Newsletter!

Join our newsletter to get our latest ebook "Ultimate JavaScript Cheat-Sheet", and Tips, Articles..

We don’t spam! Read our privacy policy for more info.

Join Our Newsletter!

Join our newsletter to get our latest ebook "Ultimate JavaScript Cheat-Sheet", and Tips, Articles..

We don’t spam! Read our privacy policy for more info.

Leave a Comment

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


The reCAPTCHA verification period has expired. Please reload the page.