Software & Finance





C Programming (Turb C++ Compiler) Displaying Monthly Calendar Given any Date from year 1900





I have given here a C Program for Displaying Monthly Calendar Given any Date from year 1900. It will validate the leap year and it will display the day (Monday, Tuesday, ...) correctly along with a * mark for the given date.


Source Code


#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

static long arrnumdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

static long arrleapnumdays[12] = { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

 

typedef enum _boolean {

      false, true

} bool;

 

bool IsLeapYear(long year)

{

    if((year % 4) == 0)

    {

        if((year % 100) == 0)

        {

          if( (year % 400) == 0)

                return true;

          else

                return false;

      }

        else

            return true;

    }

    return false;

}

 

bool CheckDate(long date, long month, long year)

{

    if(year < 0 || year > 10000)

        return false;

    if(month < 1 || month > 12)

        return false;

    if(date < 1 || date > 31)

        return false;

    if(IsLeapYear(year) == true)

    {

        if( date > arrleapnumdays[month-1])

            return false;

    }

    else

        if( date > arrnumdays[month-1])

          return false;

 

    return true;

}

 

long GetNumDays(long begdate, long begmonth, long begyear, long enddate, long endmonth, long endyear)

{

    long diffyears = endyear - begyear;

    long numdays = 0;

    long days = -1;

    long m, diffmonth, d1, d2, y;

    bool bLeap = false;

 

    if(diffyears < 0)

      return -1; // The start date is greater than end date

 

    if( CheckDate(begdate, begmonth, begyear) == false)

        return -2; // Not a valid start date

    if(CheckDate(enddate, endmonth, endyear) == false)

        return -3; // Not a valid end date

 

    if(diffyears == 0)  // same year

    {

      diffmonth = endmonth - begmonth;

      if(diffmonth < 0)

          return -1; // The start date is greater than end date

      if(diffmonth == 0)

      {

          numdays = enddate - begdate;

          if(numdays < 0)

            return -1; // The start date is greater than end date

          return numdays;

            

        }

      else

        {

            bLeap = IsLeapYear(begyear);

            // Beg date of end of Beg month

            if(bLeap == true)

                days = arrleapnumdays[begmonth - 1];

          else

                days = arrnumdays[begmonth - 1];

          numdays += days - begdate;

 

          if(diffmonth > 1)

            {

            for(m = begmonth + 1; m <= endmonth - 1; m++)

            {

                    if(bLeap == true)

                  numdays += arrleapnumdays[m - 1];

                    else

                        numdays += arrnumdays[m - 1];

                }

          }

 

          // Beg of End month to End date

            numdays += enddate;

      }

    }

    else

    {

        // Beg Date to end of beg year (Dec 31, YYYY)

        bLeap = IsLeapYear(begyear);

        if(bLeap == true)

            days = arrleapnumdays[begmonth - 1];

        else

          days = arrnumdays[begmonth - 1];

        numdays += days - begdate;

      for(d1 = begmonth + 1; d1 <= 12; d1++)

      {

          if(bLeap == true)

            numdays += arrleapnumdays[d1 - 1];

          else

            numdays += arrnumdays[d1 - 1];

      }

 

      if(diffyears > 1)

      {

          for(y = begyear + 1; y <= endyear - 1; y++)

          {

                if(IsLeapYear(y) == true)

                numdays += 366;

                else

                numdays += 365;

            }

        }

 

        // Beg of End Year (Jan 01, YYYY) to End Date

        bLeap = IsLeapYear(endyear);

      for(d2 = 1; d2 <= endmonth - 1; d2++)

      {

          if(bLeap == true)

            numdays += arrleapnumdays[d2 - 1];

          else

            numdays += arrnumdays[d2 - 1];

      }

      numdays += enddate;

    }

    return numdays;

}

 

long main()

{

    static char *strDayOfWeek[] = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };

    static char *strMonthName[] = {

      "Jan", "Feb", "Mar", "Apr",

      "May", "Jun", "Jul", "Aug",

      "Sep", "Oct", "Nov", "Dec",

    };

 

 

    char bd[128], ed[128];

    char buf[32];

    long i, j;

    long begdate, begmonth, begyear;

    long enddate, endmonth, endyear;

    long numdays1, numdays2, numdays;

    long dayofweek1, dayofweek2;

    long *pdaysinmonth = 0;

 

    begdate = 1;

    begmonth = 1;

    begyear = 1900; // Sunday

 

    ::system("cls");

    printf("Enter Date: ");

    scanf("%ld", &enddate);

    printf("Enter Month: ");

    scanf("%ld", &endmonth);

    printf("Enter Year: ");

    scanf("%ld", &endyear);

 

    sprintf(bd, "(DD/MM/YYYY) %02d/%02d/%04d", begdate, begmonth, begyear);

    sprintf(ed, "%02d/%02d/%04d", enddate, endmonth, endyear);

 

    numdays1 = GetNumDays(begdate, begmonth, begyear, 1, endmonth, endyear);

    dayofweek1 = (numdays1 + 1) % 7;

 

    if(numdays1 < 0)

    {

      if(numdays1 == -1)

          printf("The start date is greater than end date\n");

      else if(numdays1 == -2)

          printf("Not a valid start date\n");

      else if(numdays1 == -3)

          printf("Not a valid end date\n");

      return 0;

    }

 

    numdays2 = GetNumDays(begdate, begmonth, begyear, enddate, endmonth, endyear);

    dayofweek2 = (numdays2 + 1) % 7;

 

 

    pdaysinmonth = ( IsLeapYear(endyear)) ? arrleapnumdays : arrnumdays;

 

    numdays = pdaysinmonth[endmonth - 1];

 

 

    printf("\n\n");

    printf("\t%s  %ld\n\n",strMonthName[endmonth - 1], endyear);

    printf(" S   M   T   W   T   F   S\n");

    for(j = 0; j < dayofweek1; j++)

      printf("    ");

    for(i = 0; i < numdays; i++)

    {

      if(i + 1 == enddate)

          sprintf(buf, " %ld*", i + 1);

      else

          sprintf(buf, " %ld ", i + 1);

      if(i < 9)

          strcat(buf, " ");

 

      printf(buf);

      if( (i + 1 + dayofweek1) % 7 == 0)

          printf("\n");

    }

    printf("\n\n");

 

    return 0;

}

Click here to get the Turbo C Source code and executable

 

Output


Enter Date: 15

Enter Month: 12

Enter Year: 2010

 

        Dec  2010

 

 S   M   T   W   T   F   S

             1   2   3   4 

 5   6   7   8   9   10  11

 12  13  14  15* 16  17  18

 19  20  21  22  23  24  25

 26  27  28  29  30  31