Thursday, 17 July 2014

C Program to display the number of days in calendar format of an entered month of entered year

/* C Program to display the number of days in calendar format of an entered month of entered year. */



#include<stdio.h>
#include<conio.h>
void main()
{
  int i,first,distance,days,month,year;
  int find_first(int);
  clrscr();
  printf("\nEnter a month(1-12):");
  scanf("%d",&month);
  printf("\nEnter a year:");
  scanf("%d",&year);
  distance=year-2000;
  first=find_first(distance);

  for(i=1;i<=month;i++)               //calculate the 1st day of the month
   {
switch(i)
{
case 1:case 3:case 5:case 7:case 8:case 10:case 12:
days=31;
break;
case 4:case 6:case 9:case 11:
days=30;
break;
case 2:
days=(distance%4==0)?29:28;
break;      
}
if(i!=month)
first=(first+days)%7;
           else
                break;
     }
  printf("\n SUN MON TUE WED THU FRI SAT \n");   //print out   calender
  for(i=0;i<first;i++)
printf("    ");
  for(i=1;i<=days;i++)
  {
printf("%3d ",i);
if((i+first)%7==0)
printf("\n");
  }
    getch();
  }

  int find_first(int d)
  {
   enum day_of_week{sun,mon,tue,wed,thu,fri,sat};
   int leafy[7]={sat,thu,tue,sun,fri,wed,mon},f;
   if(d>0)
{ //calculate the 1st day of the year
if(d%4==0)
f=(leafy[(d/4)%7]+(d%4))%7;
else
f=(leafy[(d/4)%7]+(d%4+1))%7;
}
else
f=(leafy[(7+d/4)%7]+(7+d%4))%7;
return(f);
   }

No comments:

Post a Comment