Problem 1 (33 points): Write a C program to print a calendar for an entire year.
ID: 3753385 • Letter: P
Question
Problem 1 (33 points): Write a C program to print a calendar for an entire year. The desired year is read via keyboard. You need to calculate the first day of the year as well as whether the year is leap or not. The leap year calculation is elaborated at http://www.wikihow.com/Calculate-Leap-Years. The first day calculation can be found at http://mathforum.org/library/drmath/view/55837.html Here is a sample output for one month: January Sun Mon Tue Wed Thu Fri 2000 Sat 4 101 12 13 1415 16 17 1819 20222 23 24 25 2627 28 29 30 31 Grading: There is no need to use arrays. Comments Functioning code Well-written code Correct calendar, ordered output10 points Full submission 5 points 10 points 5 points 3 pointsExplanation / Answer
C PROGRAM
#include<stdio.h>
int days_in_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31}; // declare integer array of days in months
// declare character 2D array months names
char *months[]=
{
" ",
" January",
" February",
" March",
" April",
" May",
" June",
" July",
" August",
" September",
" October",
" November",
" December"
};
// implement dayCode() function
// used to get the day number of the first day in the year
int dayCode(int year)
{
// declare integer variables
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; // determine the day code
return daycode; // return day code
}
// implement leapYear() function
// used to find leap year or not
int leapYear(int year)
{
if(year% 4 == 0 && year%100 != 0 || year%400 == 0) // check condition leap or not
{
days_in_month[2] = 29; // add second month (march) 29 days
return 1;
}
else
{
days_in_month[2] = 28; // not leap year add 28 days into secon month (march)
return 1;
}
}
// implement Mycalendar() function with two arguments year and daycode
// used to display each month in the given year
void Mycalendar(int year, int daycode)
{
int month, day; // declare two integer variables
for ( month = 1; month <= 12; month++ ) // create for loop until 12 months of the year
{
printf("%10s %4d", months[month],year); // display month name and year
printf(" Sun Mon Tue Wed Thu Fri Sat " ); // display header of month days
// 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(" " );
}
// Set position for next month
daycode = ( daycode + days_in_month[month] ) % 7;
}
}
int main(void)
{
int year, daycode, leapyear;
printf("Enter Year: ");
scanf("%d",&year); // read year
daycode = dayCode(year); // calling dayCode() function and return daycode
leapYear(year); // calling leapYear() function and receive leap year or not leap year
Mycalendar(year, daycode); // calling Mycalendar() function
printf(" ");
}
OUTPUT
Enter Year: 2000
January 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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 2000
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.