Here is the code which displays the Calendar of the month entered by the user.
Test Run output
Code: C
#include<stdio.h>
#include<conio.h>
#define Y 1900 /*1/1/1900 monday*/
void days(int *,int *);
void display(int *, int);
int _MONTH[]={{31},{28},{31},{30},{31},{30},{31},{31},{30},{31},{30},{31}};
void main()
{
char ch = 'y';
int m,yr;
do
{
printf("\nEnter month [1-12] ");
scanf("%ld",&m);
printf("Enter year ");
scanf("%ld",&yr);
if((yr%4==0)&&((yr%100!=0)||(yr%400==0)))
_MONTH[1]=29;
days(&m,&yr);
printf("\n\n\n\nPress 'x' to EXIT\n");
fflush(stdin);
ch = getch();
}while(ch != 'x');
}
void days(int *m,int *yr)
{
int i,md=0,leap=0,track;
unsigned int d,yrd;
for(i=Y;i<*yr;i++)
{
if((i%4==0)&&((i%100!=0)||(i%400==0)))
leap++;
}
for(i=0;i<(*m-1)&&(i<11);i++)
md=md+_MONTH[i];
yrd=(*yr-Y)*365;
d=yrd+leap+md;
track=d%7;
display(&track,*m);
}
void display(int *track,int m)
{
int t,dt,loop;
t=*track;
printf("\n\n\n\t\t");
printf("MON\tTUES\tWED\tTHURS\tFRI\tSAT\tSUN\n\n");
for(loop=0;loop<t+2;loop++) /*t+2 due to two additional \t*/
printf("\t");
for(dt=1;dt<=_MONTH[m-1];dt++)
{
if(t%7==0 && t!=0)
printf("\n\n\t\t");
printf("%d",dt);
printf("\t");
t++;
}
}
Code:
Enter month [1-12] 12
Enter year 1979
MON TUES WED THURS FRI SAT SUN
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
Press 'x' to EXIT


