Create 2010 Calendar In C

17/01/2010 ·

This program will make and print a calendar for the year 2010.


#include <stdio.h>
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=2010;
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:


1 comments:

drvspatel said...
18/05/2010, 00:33  

Wonderful...thanks a lot

Post a Comment

Submit Request

If you need some code or programs just post a comment on this post. I will try to provide you the same at the earliest.

About this blog

Free sample code, example code , code snippet , tutorials in C C++. Find, download and reuse the code database available which vary from small programs to large ones as well. Feel free to request for code that is not in the list.

Followers