This program will make and print a calendar for any year starting from 1800 AD. However 1800 is not a fixed starting point, you can tweak the code and change firstYear, dayOffset and firstLeapYear to get any starting point. The dayoffset is the first day of our start year which may not be a Sunday ( in 1800 it was Wednesday). The logic goes like that it first calculates the number of days passed from a starting point (1800 AD). The days passed is then divided by 7 to get the starting day for a particular month. Then it gets the number of days in that month and loops accordingly to print the number of days. The example here prints the calendar for 2009. Just change the year variable in main function to print calendar of that particular year.
#include
int IsLeapYear(int Year)
{
if (Year % 400 == 0 || (Year % 4 == 0 && Year % 100 != 0))
return 1;
return 0;
}
int DaysPassedFromYear(int firstYear, int month,int year)
{
int days;
int daysPassed[] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; //per month per year
const int dayOffset = 3; //The first day of our start year may not be a Sunday ( in 1800 it was Wednesday)
const int firstLeapYear = 1804; //help to reduce how many leap years we have to check
int count;
//calculates basic number of days passed
days = (year - firstYear) * 365;
days += dayOffset;
days += daysPassed[month-1];
//add on the extra leapdays for past years
for (count = firstLeapYear; count < year ; count +=4)
{
if (IsLeapYear(count) )
{
days++;
}
}
//add leapday for this year if requested month is greater than Feb
if( month > 2 && IsLeapYear(year) )
{
days++;
}
return days;
}
int GetNumberOfDaysInMonth(int Month,int Year)
{
int NumberOfDaysInMonth;
printf("\n");
switch (Month)
{
case 1:
printf(" January %d\n",Year);
NumberOfDaysInMonth = 31;
break;
case 2:
printf(" February %d\n",Year);
if(IsLeapYear(Year))
NumberOfDaysInMonth = 29;
else
NumberOfDaysInMonth = 28;
break;
case 3:
printf(" March %d\n",Year);
NumberOfDaysInMonth = 31;
break;
case 4:
printf(" April %d\n",Year);
NumberOfDaysInMonth = 30;
break;
case 5:
printf(" May %d\n",Year);
NumberOfDaysInMonth = 31;
break;
case 6:
printf(" June %d\n",Year);
NumberOfDaysInMonth = 30;
break;
case 7:
printf(" July %d\n",Year);
NumberOfDaysInMonth = 31;
break;
case 8:
printf(" August %d\n",Year);
NumberOfDaysInMonth = 31;
break;
case 9:
printf(" September %d\n",Year);
NumberOfDaysInMonth = 30;
break;
case 10:
printf(" October %d\n",Year);
NumberOfDaysInMonth = 31;
break;
case 11:
printf(" November %d\n",Year);
NumberOfDaysInMonth = 30;
break;
case 12:
printf(" December %d\n",Year);
NumberOfDaysInMonth = 31;
break;
}
printf("__________________________________________");
printf("\n Sun Mon Tue Wed Thu Fri Sat\n\n");
return NumberOfDaysInMonth;
}
int main()
{
int year=2009;
int month;
for(month=1;month<=12;month++)
{
const int firstYear = 1800; //This is our start point
int NumDaysInMonth=GetNumberOfDaysInMonth(month,year);
int firstDayOfMonth = DaysPassedFromYear(firstYear,month,year) % 7;//0-Sunday 1-Monday
firstDayOfMonth++;//offsetting so that 1-Sunday,2-monday etc
int i;
for(i=1;i{
printf(" ");
if(i%7 == 0) printf("\n");
}
for(i=firstDayOfMonth;i{
if((i-firstDayOfMonth) < 9)
printf(" %d ",i-firstDayOfMonth+1);
else
printf(" %d ",i-firstDayOfMonth+1);
if(i%7 == 0) printf("\n");
}
printf("\n");
}
return 0;
}
compiled on:gcc and linux
output:
9 comments:
cant the output be more formatted?
The code will print a proper formatted output. The formatting is only removed in the blogspot. I will try and paste the exact formatted output soon...
Sharad
thanks .. nice code
Thanks a lot.looking for it for a long time.
Could you please post it for the current year or where to change?
Hello am Patrick please how can i make the foreground of the public holiday to be red
This is absolutely awesome Blog..calendar printing services in hyderabad Great stuff, will looking forward for some more postings
calendar printing in hyderabad
calendar printers in Hyderabad
Please let me know if you’re looking for an author for your site. You can also check my blog for : Calendar printing
Post a Comment