Back

#include<stdio.h>

static char daytab[2][13] =
  {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31},
    {0, 31, 29, 31, 30 ,31, 31, 31, 31, 30, 31, 30, 31},
  };

int day_of_year(int year, int month, int day);
void month_day(int year, int yearday, int *pmonth, int *pday);
char *month_name(int n);

int main()

{
int month=0, day=0;
month_day(2007, 100, &month, &day);
printf("Number of days from Jan 1-2007 to Nov 11 -2007 is : %d\n",day_of_year(2007,11,11));
printf("The month and day of 100 days from Jan 1-2007 is: %s  %d 2007 \n",month_name(month),day);

return 0;
}

int day_of_year(int year, int month, int day)
 {
   int i, leap;

   leap = (year%4==0 && year%100 != 0 || year%400 == 0);

   for(i=1; i < month; i++)
    day += daytab[leap][i];

   return day;
}

void month_day(int year, int yearday, int *pmonth, int *pday)
 {
   int i, leap;

   leap = ( year%4==0 && year%100 != 0 || year %400 ==0);
   for(i = 1; yearday > daytab[leap][i]; i++)
     yearday -= daytab[leap][i];
   *pmonth = i;
   *pday = yearday;
 }

char *month_name(int n)
 {
   static char *name[] =
     {
        "Illegal month", "January", "February", "March", "April", "May", "June", "July",
        "August", "September", "October", "November", "December"
     };

   return (n<1 || n>12) ? name[0] : name[n];
 }

Top