Calendar in C: C Mini Project

The mini-project “Calendar in C” is a console application using the C programming language. This project compiles in Code Blocks with the GCC compiler. In this console application, you can check the date for the given month and year.

In this project, we are using Zeller’s Congruence. Zeller’s congruence is an algorithm devised by Christian Zeller in the 19th century to calculate the day of the week for any Julian or Gregorian calendar date. It can be considered to be based on the conversion between Julian day and the calendar date.

Check the below C code for the calendar. The below code allows you to navigate year or month using the arrow key of your keyboard. Later I will update the explanation of the code.

#include<stdio.h>

//check leap year
#define IS_LEAP_YEAR(Year) ((Year%4==0 && Year%100 != 0)||Year%400==0)
/*Function returns the index of the days
of the given month and year using ZELLER'S ALGORITHM
  For e.g-
  Index     Day
  0         Sunday
  1         Monday
  2         Tuesday
  3         Wednesday
  4         Thursday
  5         Friday
  6         Saturday*/
int getZeller(int Month,int Year)
{
    int Day = 1, ZMonth, ZYear, Zeller;
    if(Month < 3)
        ZMonth = Month+10;
    else
        ZMonth = Month-2;
    if(ZMonth > 10)
        ZYear = Year-1;
    else
        ZYear = Year;
    Zeller = ((int)((13*ZMonth-1)/5)+Day+ZYear%100+
              (int)((ZYear%100)/4)-2*(int)(ZYear/100)+
              (int)(ZYear/400)+77)%7;
    return Zeller;
}

/* A Function to return the number of days in
   a month
  Month Number     Name        Number of Days
  0                January     31
  1                February    28 (non-leap) / 29 (leap)
  2                March       31
  3                April       30
  4                May         31
  5                June        30
  6                July        31
  7                August      31
  8                September   30
  9                October     31
  10               November    30
  11               December    31
*/
int getNumberOfDays(int monthNumber, int year)
{
    int MonthDay[12] = {31,28,31,30,31,30,31,31,30,31,30,31};
    MonthDay[monthNumber-1] += (IS_LEAP_YEAR(year) && (monthNumber == 2));
    return (MonthDay[monthNumber-1]);
}

//Align the message
void printMessageCenter(const char* message)
{
    int len =0;
    int pos = 0;
    //calculate how many space need to print
    len = (78 - strlen(message))/2;
    printf("\t\t\t");
    for(pos =0 ; pos < len ; pos++)
    {
        //print space
        printf(" ");
    }
    //print message
    printf("%s",message);
}


// Function to print the calendar of the given year
void printCalendar(int month,int year)
{
//get month name
    char * months[] = {"January", "February", "March",
                       "April", "May", "June",
                       "July", "August", "September",
                       "October", "November", "December"
                      };
    char buf[32] = {0};
    int j,k;
    sprintf(buf,"%s%d\n\n","Calendar - ",year);
    printMessageCenter(buf);
    int days;
    // Index of the day from 0 to 6
    int current = getZeller(month,year);
    days = getNumberOfDays (month, year);
    // Print the current month name
    printMessageCenter(months[month-1]);
    printf("\n");
    // Print days in the columns
    printf("  Sun  Mon  Tue  Wed  Thu  Fri  Sat\n");
    // Print appropriate spaces
    for (k = 0; k < current; k++)
        printf("     ");
    //print days as per the month
    for (j= 1; j <= days; j++)
    {
        printf("%5d", j);
        ++k; //change new line for each week
        if (k > 6)
        {
            k = 0;
            printf("\n");
        }
    }
    if (k)
        printf("\n");
    current = k;
}

void headMessage(const char *message)
{
    system("cls");
    printf("\t\t\t###########################################################################");
    printf("\n\t\t\t############                                                   ############");
    printf("\n\t\t\t############        C Project: Calendar For Given Month.       ############");
    printf("\n\t\t\t############                                                   ############");
    printf("\n\t\t\t###########################################################################");
    printf("\n\t\t\t---------------------------------------------------------------------------\n");
    printMessageCenter(message);
    printf("\n\t\t\t----------------------------------------------------------------------------");
}

void welcomeMessage()
{
    headMessage("www.aticleworld.com");
    printf("\n\n\n\n\n");
    printf("\n\t\t\t  **-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n");
    printf("\n\t\t\t        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    printf("\n\t\t\t        =                  Program                  =");
    printf("\n\t\t\t        =                   Shows                   =");
    printf("\n\t\t\t        =               calendar of a               =");
    printf("\n\t\t\t        =                Given Month                =");
    printf("\n\t\t\t        =                                           =");
    printf("\n\t\t\t        =-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=");
    printf("\n\t\t\t  **-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**-**\n");
    printf("\n\n\n\t\t\t Enter any key to continue.....");
    getchar();
    system("cls");
    printf("\n\tThis program shows calendar of a given month.\n\n");
}

int main()
{
    int Month, Year;
    welcomeMessage();
    //validate input
    while(1)
    {
        fflush(stdin);
        printf("Enter the Year YYYY = ");
        scanf("%d",&Year);
        printf("Enter the Month = ");
        scanf("%d", &Month);
        if(Year < 0)
        {
            printf("\nInvalid year value...");
            continue;
        }
        if(Year < 100)
            Year += 1900;
        if(Year < 1582 || Year > 4902)
        {
            printf("\nInvalid year value...");
            continue;
        }
        if(Month < 1 || Month > 12)
        {
            printf("\nInvalid month value...");
            continue;
        }
        break;
    }  /*Break loop after valid input*/
    //clean console
    system("cls");
    do
    {
        printCalendar(Month,Year);
        //Display message
        printf("\n\n\t\t(*) Press Up Arrow for Next Year.");
        printf("\n\n\t\t(*) Press Down Arrow for Previous Year.");
        printf("\n\n\t\t(*) Press Right Arrow for Next Month.");
        printf("\n\n\t\t(*) Press Left Arrow for Previous Month.");
        printf("\n\n\t\t(*) Press ESC for Exit.\n\n\n\t\t");
        char KeyCode = getch();         /* getting Key Code */
        if(KeyCode == 72)           /* Up Arrow */
            Year++;
        if(KeyCode == 80)           /* Down Arrow */
            Year--;
        if(KeyCode == 77)           /* Right Arrow */
        {
            Month++;
            if(Month > 12)
            {
                Month = 1;
                Year++;
            }
        }
        if(KeyCode == 75)            /* Left Arrow */
        {
            Month--;
            if(Month < 1)
            {
                Month = 12;
                Year--;
            }
        }
        if(KeyCode == 27)            /* Esc button */
        {
            system("cls");
            printf("\n\n\n\n\n\t\tBye Bye ...");
            printf("\n\t\tThanks for reading Aticleworld\n\n\n\n\n\n");
            exit(1);
        }
        system("cls");
    }
    while(1);
    return (0);
}

Output:

Screen 1:

This is the first welcome screen that asks you to press any key on your keyboard.

Calendar using the code

 

Screen 2:

After pressing the key on your keyboard a second screen will prompt. The second screen will ask you to enter the valid year and month.

c program for calendar

 

Screen 3:

This screen will show the date and day according to your input. This screen also allows you to see the day and date of the next and previous year and month; you just need to press the arrow button as per the description.

For example, if you want to see the calendar for the next year you just need to press the “up arrow (↑)” button.

calendar project in C

 

Recommended posts: