Write a function which would generate a calendar for a given month. The function
ID: 3803031 • Letter: W
Question
Write a function which would generate a calendar for a given month. The function shall have two input parameters: a character identifying the first day of the month, and an integer number of days in the month (from 28 to 31). To identify the first day, use the first letter of that day, except for Thursday and Sunday. Use 'h' or 'H' for Thursday and 'u' or 'U' for Sunday, to distinguish them from Tuesday and Saturday, respectively. Test the function by calling it at least twice with different parameters.Explanation / Answer
Please refer below code
#include<stdlib.h>
#include<stdio.h>
void print_calender(char first_day, int number_of_days)
{
int day,date=1,j,i;
//converting character to day index
if(first_day == 'u' || first_day == 'U')
day = 0;
else if(first_day == 'm' || first_day == 'M')
day = 1;
else if(first_day == 't' || first_day == 'T')
day = 2;
else if(first_day == 'w' || first_day == 'W')
day = 3;
else if(first_day == 'h' || first_day == 'H')
day = 4;
else if(first_day == 'f' || first_day == 'F')
day = 5;
else if(first_day == 's' || first_day == 'S')
day = 6;
else
printf(" Invalid day ");
printf(" Sun Mon Tue Wed Thurs Fri Sat ");
for (j=1;date<=number_of_days;++j, date++)
{
//for first row we have to give tab untill day column comes
if (j==1)
{
for (i=1;i<=day;i++)
printf(" ");
}
//printing date under that column
printf("%d ", date);
//if count goes beyond 6 then we have to go to new line
if ((day+j)%7==0)
printf(" ");
}
}
int main()
{
char day;
int number_of_days;
printf("Enter First Day of month : ");
scanf("%c", &day);
printf("Enter number of days in month : ");
scanf("%d", &number_of_days);
print_calender(day,number_of_days);
return 0;
}
Please refer below output
Enter First Day of month : U
Enter number of days in month : 31
Sun Mon Tue Wed Thurs 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
Process returned 0 (0x0) execution time : 3.815 s
Press any key to continue.
2)
Enter First Day of month : w
Enter number of days in month : 30
Sun Mon Tue Wed Thurs 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
Process returned 0 (0x0) execution time : 7.017 s
Press any key to continue.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.