Code:
#include <stdio.h>
#define TRUE 1
#define FALSE 0
#define MIN_YEAR 1900
#define MAX_YEAR 2100
#define START_DAY 2
#define DAYS_IN_WEEK 7
#define MONTHS_IN_YEAR 12
int checkYear (void);
int isLeap (int);
int calcStartDayYear (int);
int printCalender (int, int, int[], int, int);
int dayCount1 = 0;
int dayCount2 = 0;
int dayCount3 = 0;
int main()
{
int days[] = {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};
int year = 0;
int leapResult = 0;
int startDay = 0;
int monthsInRow = 0;
int daysInRow = 0;
printf("\nEnter a year between 1900 and 2100 : ");
year = checkYear();
leapResult = isLeap(year);
startDay = calcStartDayYear(year);
printf("\n\n\t\t\t\t\t%d\n\n", year);
for(monthsInRow = 0; monthsInRow <12; monthsInRow += 3)
{
switch(monthsInRow)
{
case 0:
dayCount1 = 0;
dayCount2 = 0;
dayCount3 = 0;
printf("\tJan\t \t\t\tFeb\t \t\t\tMar\n");
break;
case 3:
dayCount1 = 0;
dayCount2 = 0;
dayCount3 = 0;
printf("\tApr\t \t\t\tMay\t \t\t\tJun\n");
break;
case 6:
dayCount1 = 0;
dayCount2 = 0;
dayCount3 = 0;
printf("\tJul\t \t\t\tAug\t \t\t\tSep\n");
break;
case 9:
dayCount1 = 0;
dayCount2 = 0;
dayCount3 = 0;
printf("\tOct\t \t\t\tNov\t \t\t\tDec\n");
break;
}
printf(" S M Tu W Th F S\t\t");
printf(" S M Tu W Th F S\t\t");
printf(" S M Tu W Th F S\n");
for(daysInRow = 0; daysInRow < 6; daysInRow++)
{
startDay = printCalender(monthsInRow + 1, year, days, startDay, daysInRow);
startDay = printCalender(monthsInRow + 2, year, days, startDay, daysInRow);
startDay = printCalender(monthsInRow + 3, year, days, startDay, daysInRow);
printf("\n");
}
printf("\n");
}
printf("\n\n");
return 0;
}
int checkYear()
{
int year = 0;
scanf("%d", &year);
while( (year < MIN_YEAR) || (year > MAX_YEAR) )
{
printf("\nPlease enter year between 1900 and 2100 : ");
scanf("%d", &year);
}
return year;
}
int isLeap(int year)
{
if( (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)) )
{
return TRUE;
}
return FALSE;
}
int calcStartDayYear(int year)
{
int yearCount = 0;
int startDayNextYear = START_DAY;
for(yearCount = MIN_YEAR; yearCount < year; yearCount++)
{
if( isLeap(yearCount) )
{
startDayNextYear += 2;
}
else
{
startDayNextYear += 1;
}
if(startDayNextYear == 8)
{
startDayNextYear = 1;
}
else if(startDayNextYear == 9)
{
startDayNextYear = 2;
}
}
return startDayNextYear;
}
int printCalender(int month, int year, int days[], int startDay, int daysInRow)
{
int daysInMonth[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int dayOfWeek[] = {1, 2, 3, 4, 5, 6, 7};
int colCount = 0;
int index = 0;
if(daysInRow == 0)
{
while(dayOfWeek[index] != startDay)
{
printf(" ");
index++;
colCount++;
}
if( (month == 2) && (isLeap(year)) )
{
daysInMonth[month] = 29;
}
startDay = (startDay + daysInMonth[month]) % DAYS_IN_WEEK;
if(startDay == 0)
{
startDay = 7;
}
}
while(colCount <= 6)
{
if( (month == 1) || (month == 4) || (month == 7) || (month == 10) )
{
if(dayCount1 < daysInMonth[month])
{
printf(" %2d", days[dayCount1]);
dayCount1++;
colCount++;
}
else
{
while(colCount <= 6)
{
printf(" ");
colCount++;
}
}
}
else if( (month == 2) || (month == 5) || (month == 8) || (month == 11) )
{
if( (month == 2) && (isLeap(year)) )
{
daysInMonth[month] = 29;
}
if(dayCount2 < daysInMonth[month])
{
printf(" %2d", days[dayCount2]);
dayCount2++;
colCount++;
}
else
{
while(colCount <= 6)
{
printf(" ");
colCount++;
}
}
}
else
{
if(dayCount3 < daysInMonth[month])
{
printf(" %2d", days[dayCount3]);
dayCount3++;
colCount++;
}
else
{
while(colCount <= 6)
{
printf(" ");
colCount++;
}
}
}
}
printf("\t\t");
return startDay;
}