Advertisement
apl-mhd

calender

Aug 6th, 2016
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.05 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. #define TRUE    1
  4. #define FALSE   0
  5.  
  6. int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
  7. char *months[]=
  8. {
  9.     " ",
  10.     "\n\n\nJanuary",
  11.     "\n\n\nFebruary",
  12.     "\n\n\nMarch",
  13.     "\n\n\nApril",
  14.     "\n\n\nMay",
  15.     "\n\n\nJune",
  16.     "\n\n\nJuly",
  17.     "\n\n\nAugust",
  18.     "\n\n\nSeptember",
  19.     "\n\n\nOctober",
  20.     "\n\n\nNovember",
  21.     "\n\n\nDecember"
  22. };
  23.  
  24.  
  25. int inputyear(void)
  26. {
  27.     int year;
  28.  
  29.     printf("Please enter a year (example: 1999) : ");
  30.     scanf("%d", &year);
  31.     return year;
  32. }
  33.  
  34. int determinedaycode(int year)
  35. {
  36.     int daycode;
  37.     int d1, d2, d3;
  38.  
  39.     d1 = (year - 1.)/ 4.0;
  40.     d2 = (year - 1.)/ 100.;
  41.     d3 = (year - 1.)/ 400.;
  42.     daycode = (year + d1 - d2 + d3) %7;
  43.     return daycode;
  44. }
  45.  
  46.  
  47. int determineleapyear(int year)
  48. {
  49.     if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
  50.     {
  51.         days_in_month[2] = 29;
  52.         return TRUE;
  53.     }
  54.     else
  55.     {
  56.         days_in_month[2] = 28;
  57.         return FALSE;
  58.     }
  59. }
  60.  
  61. void calendar(int year, int daycode)
  62. {
  63.     int month, day;
  64.     for ( month = 1; month <= 12; month++ )
  65.     {
  66.         printf("%s", months[month]);
  67.         printf("\n\nSun  Mon  Tue  Wed  Thu  Fri  Sat\n" );
  68.  
  69.         // Correct the position for the first date
  70.         for ( day = 1; day <= 1 + daycode * 5; day++ )
  71.         {
  72.             printf(" ");
  73.         }
  74.  
  75.         // Print all the dates for one month
  76.         for ( day = 1; day <= days_in_month[month]; day++ )
  77.         {
  78.             printf("%2d", day );
  79.  
  80.             // Is day before Sat? Else start next line Sun.
  81.             if ( ( day + daycode ) % 7 > 0 )
  82.                 printf("   " );
  83.             else
  84.                 printf("\n " );
  85.         }
  86.             // Set position for next month
  87.             daycode = ( daycode + days_in_month[month] ) % 7;
  88.     }
  89. }
  90.  
  91.  
  92. int main(void)
  93. {
  94.     int year, daycode, leapyear;
  95.  
  96.     year = inputyear();
  97.     daycode = determinedaycode(year);
  98.     determineleapyear(year);
  99.     calendar(year, daycode);
  100.     printf("\n");
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement