0% found this document useful (0 votes)
52 views7 pages

Calendar

This C program contains functions to determine if a year is a leap year, calculate the number of days passed since a starting year, get the number of days in a given month, and print out a calendar for a given year. It includes arrays to store the number of days in each month and the days passed each year. It uses modulo operations to determine the first day of the month and print the calendar with proper formatting.

Uploaded by

Priyanka Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views7 pages

Calendar

This C program contains functions to determine if a year is a leap year, calculate the number of days passed since a starting year, get the number of days in a given month, and print out a calendar for a given year. It includes arrays to store the number of days in each month and the days passed each year. It uses modulo operations to determine the first day of the month and print the calendar with proper formatting.

Uploaded by

Priyanka Gupta
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Calendar.

c
#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 wa s 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 1Monday firstDayOfMonth++;//offsetting so that 1-Sunday,2-monday etc int i; for(i=1;i<firstdayofmonth;i++) <br=""> { printf(" "); if(i%7 == 0) printf("\n"); } for(i=firstDayOfMonth;i<numdaysinmonth+firstdayofmonth;i++) <br=""> { 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; }

You might also like