Notification texts go here Contact Us Click here

Calendar display projects using C language

Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated

  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. int main(void)
  92. {
  93. int year, daycode, leapyear;
  94.  
  95. year = inputyear();
  96. daycode = determinedaycode(year);
  97. determineleapyear(year);
  98. calendar(year, daycode);
  99. printf("\n");
  100. }
  101.  
  102. OUTPUT :
  103.  
  104. Please enter a year (example: 1999) : 2000
  105. January
  106.  
  107. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  108.                                 1
  109.   2    3    4    5    6    7    8
  110.   9   10   11   12   13   14   15
  111.  16   17   18   19   20   21   22
  112.  23   24   25   26   27   28   29
  113.  30   31   
  114.  
  115.  
  116. February
  117.  
  118. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  119.             1    2    3    4    5
  120.   6    7    8    9   10   11   12
  121.  13   14   15   16   17   18   19
  122.  20   21   22   23   24   25   26
  123.  27   28   29   
  124.  
  125.  
  126. March
  127.  
  128. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  129.                  1    2    3    4
  130.   5    6    7    8    9   10   11
  131.  12   13   14   15   16   17   18
  132.  19   20   21   22   23   24   25
  133.  26   27   28   29   30   31   
  134.  
  135.  
  136. April
  137.  
  138. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  139.                                 1
  140.   2    3    4    5    6    7    8
  141.   9   10   11   12   13   14   15
  142.  16   17   18   19   20   21   22
  143.  23   24   25   26   27   28   29
  144.  30   
  145.  
  146.  
  147. May
  148.  
  149. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  150.        1    2    3    4    5    6
  151.   7    8    9   10   11   12   13
  152.  14   15   16   17   18   19   20
  153.  21   22   23   24   25   26   27
  154.  28   29   30   31   
  155.  
  156.  
  157. June
  158.  
  159. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  160.                       1    2    3
  161.   4    5    6    7    8    9   10
  162.  11   12   13   14   15   16   17
  163.  18   19   20   21   22   23   24
  164.  25   26   27   28   29   30   
  165.  
  166.  
  167. July
  168.  
  169. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  170.                                 1
  171.   2    3    4    5    6    7    8
  172.   9   10   11   12   13   14   15
  173.  16   17   18   19   20   21   22
  174.  23   24   25   26   27   28   29
  175.  30   31   
  176.  
  177.  
  178. August
  179.  
  180. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  181.             1    2    3    4    5
  182.   6    7    8    9   10   11   12
  183.  13   14   15   16   17   18   19
  184.  20   21   22   23   24   25   26
  185.  27   28   29   30   31   
  186.  
  187.  
  188. September
  189.  
  190. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  191.                            1    2
  192.   3    4    5    6    7    8    9
  193.  10   11   12   13   14   15   16
  194.  17   18   19   20   21   22   23
  195.  24   25   26   27   28   29   30
  196.  
  197.  
  198.  
  199. October
  200.  
  201. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  202.   1    2    3    4    5    6    7
  203.   8    9   10   11   12   13   14
  204.  15   16   17   18   19   20   21
  205.  22   23   24   25   26   27   28
  206.  29   30   31   
  207.  
  208.  
  209. November
  210.  
  211. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  212.                  1    2    3    4
  213.   5    6    7    8    9   10   11
  214.  12   13   14   15   16   17   18
  215.  19   20   21   22   23   24   25
  216.  26   27   28   29   30   
  217.  
  218.  
  219. December
  220.  
  221. Sun  Mon  Tue  Wed  Thu  Fri  Sat
  222.                            1    2
  223.   3    4    5    6    7    8    9
  224.  10   11   12   13   14   15   16
  225.  17   18   19   20   21   22   23
  226.  24   25   26   27   28   29   30
  227.  31   
  228.  
Explanation : To begin, we'll create two arrays: one with the number of days in each month, and another with all of the month names. Note that the first position in both arrays is intentionally left empty; we want to keep things simple by using 1 to 12. The user input is obtained via the first function inputyear(). The user is asked to enter a year. Note that no input validation or error handling is done in order to keep things basic. The following method, determinedaycode(), is used to get the day number of the first day of that year, allowing us to display the date in the proper location. (As a result, it's just utilized for output.) The determineleapyear() method is used to see whether the user's input is a leap year. If this is the case, the number of days in February is increased to 29. Each month is printed on the screen using the final function calendar(). To loop across all months, use the first for loop. The month's name and all of the days of the week are then printed. The daycode is then used to place the prompt under the correct weekday. Then we print a month's worth of dates. The final step is to place the prompt in the proper weekday position.

Post a Comment

Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.