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.
- #include<stdio.h>
- #define TRUE 1
- #define FALSE 0
- int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};
- char *months[]=
- {
- " ",
- "\n\n\nJanuary",
- "\n\n\nFebruary",
- "\n\n\nMarch",
- "\n\n\nApril",
- "\n\n\nMay",
- "\n\n\nJune",
- "\n\n\nJuly",
- "\n\n\nAugust",
- "\n\n\nSeptember",
- "\n\n\nOctober",
- "\n\n\nNovember",
- "\n\n\nDecember"
- };
- int inputyear(void)
- {
- int year;
- printf("Please enter a year (example: 1999) : ");
- scanf("%d", &year);
- return year;
- }
- int determinedaycode(int year)
- {
- int daycode;
- int d1, d2, d3;
- d1 = (year - 1.)/ 4.0;
- d2 = (year - 1.)/ 100.;
- d3 = (year - 1.)/ 400.;
- daycode = (year + d1 - d2 + d3) %7;
- return daycode;
- }
- int determineleapyear(int year)
- {
- if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)
- {
- days_in_month[2] = 29;
- return TRUE;
- }
- else
- {
- days_in_month[2] = 28;
- return FALSE;
- }
- }
- void calendar(int year, int daycode)
- {
- int month, day;
- for ( month = 1; month <= 12; month++ )
- {
- printf("%s", months[month]);
- printf("\n\nSun Mon Tue Wed Thu Fri Sat\n" );
- // Correct the position for the first date
- for ( day = 1; day <= 1 + daycode * 5; day++ )
- {
- printf(" ");
- }
- // Print all the dates for one month
- for ( day = 1; day <= days_in_month[month]; day++ )
- {
- printf("%2d", day );
- // Is day before Sat? Else start next line Sun.
- if ( ( day + daycode ) % 7 > 0 )
- printf(" " );
- else
- printf("\n " );
- }
- // Set position for next month
- daycode = ( daycode + days_in_month[month] ) % 7;
- }
- }
- int main(void)
- {
- int year, daycode, leapyear;
- year = inputyear();
- daycode = determinedaycode(year);
- determineleapyear(year);
- calendar(year, daycode);
- printf("\n");
- }
- OUTPUT :
- Please enter a year (example: 1999) : 2000
- January
- Sun Mon Tue Wed Thu Fri Sat
- 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
- February
- Sun Mon Tue Wed Thu Fri Sat
- 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
- March
- Sun Mon Tue Wed Thu Fri Sat
- 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
- April
- Sun Mon Tue Wed Thu Fri Sat
- 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
- May
- Sun Mon Tue Wed Thu Fri Sat
- 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
- June
- Sun Mon Tue Wed Thu Fri Sat
- 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
- July
- Sun Mon Tue Wed Thu Fri Sat
- 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
- August
- Sun Mon Tue Wed Thu Fri Sat
- 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
- September
- Sun Mon Tue Wed Thu Fri Sat
- 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
- October
- Sun Mon Tue Wed Thu Fri Sat
- 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
- November
- Sun Mon Tue Wed Thu Fri Sat
- 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
- December
- Sun Mon Tue Wed Thu Fri Sat
- 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
Calendar display projects using C language
Please wait 0 seconds...
Scroll Down and click on Go to Link for destination
Congrats! Link is Generated