Thinking in C++

My C++ code snips.

Calendar in C.

Posted by Abhi on July 7, 2012

I just finished writing the code for printing a calendar for a given year in C. Thought it might be useful to someone.

Program to print the calendar of a given
year. You also have to enter the starting
day of that year in words.

Written by Abijeet. 7-7-2012



#define DAYS 7
#define WEEKS 5
#define MAX 30
#define MONTHS 12

int isLeapYear(int);
int getStartingDay(const char*);
int noOfDaysInMonth(int);
int printMonth(int,int,int,int);
char* getNameOfMonth(int);

int main()
    int monthNo, currentDay, year, startDay, lastDay;   // startDay is starting day, lastDay is the last day of the month.
    int flagLeap,errNo;
    char startingDay[MAX],ans;
        printf("Enter the year: ");
        // Determining whether the year is a leap year.
        flagLeap = isLeapYear(year);

        // Determining the starting day of the week
                printf("\nWrong day entered, please enter the starting day as Sunday or Monday or Tuesday or any other day of the week.\n\n");

            printf("Please enter the starting day of the year: ");

            startDay = getStartingDay(startingDay);

        }while(startDay == -1);
        //printf("%d %d %d",startDay,flagLeap,noOfDaysInMonth(7));

        // Prints the months of the year
            // For Feburary in which the year is a leap year.
            if((monthNo == 1) && flagLeap)
                lastDay = printMonth(monthNo,startDay,29,year);
            // Calculating the starting day of the next month.
            if(lastDay == 6)
                startDay = 0;
                startDay = lastDay + 1;
        printf("Do you want to display the calendar for another year? (Y/N) : __\b\b");

    printf("\nThanks for using..Bye!\n\n");

    return 0;

Function to check if the year is a
leap year. Return 1 if it is a leap
year else Returns 0.


int isLeapYear(int year)
        return 1;
    return 0;

Function to return the number of the day
in the week when the user enters the
string. For example, will return 0 for
Sunday, 1 for Monday, 2 for Tuesday and
so on.....


int getStartingDay(const char* nameOfDay)
    char nameOfDaysOfWeek[DAYS][MAX] = {"Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"};
    char nameOfDaysOfWeekInSmall[DAYS][MAX] = {"sunday","monday","tuesday","wednesday","thursday","friday","saturday"};
    int i;
        if(strcmp(nameOfDay,nameOfDaysOfWeek[i])==0 || strcmp(nameOfDay,nameOfDaysOfWeekInSmall[i])==0)
            return i;
    return -1;

Function to return the number of
days in a month. Returns 28 for
Feburary by default.


int noOfDaysInMonth(int monthNum)
    int dayMonth[MONTHS][1] = {{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};

    return *dayMonth[monthNum];

Core of the program. This function takes
month number, starting day, and total days
in a month as input and displays the month.
It also returns the name of the last day of
the month so that the calling function can
use it to calculate the first day of the
next month. Also takes the year as input
and prints it.


int printMonth(int month,int startDay,int noOfDays,int year)
    int currentDay=1,calendar[WEEKS][DAYS],currentNameOfDay;
    int row,col;
    char nameOfMonth[MAX];

    // Initializing all the array elements to 0

    row = 0;
    // Filling up the array with dates
    while(currentDay     {
        for(col = 0;colnoOfDays)

        ++row; // Moving to the next row of the array.
    currentNameOfDay = col;

    Stores where the current month ended, and returns it to the calling functions so that it
    can calculate the starting day of the next month!

    // Now printing the calendar!!
    printf("\n%s, %d\n__________________________________________________________________\n",nameOfMonth,year);

    // Printing the days of the week....

    for(row=0; row    {
        for(col=0;col < DAYS;++col)
        We initialized the calendar array to 0, now when printing it we do not
        want to display the 0's. So if the value is 0, we print a tab space and
            if(calendar[row][col] == 0)
                printf(" \t");

    return currentNameOfDay;

Takes the month number as input and
returns the name of the month.


char* getNameOfMonth(int month)
    static char monthNames[MONTHS][MAX]={"January","Feburary","March","April","May","June","July","August","September","October","November","December"};
    return monthNames[month];


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: