Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C program question Write a program that prompts the user for any year higher or

ID: 3830973 • Letter: C

Question

C program question Write a program that prompts the user for any year higher or equal to 1900 and prints its monthly calendar. The calendar runs through the entire year displaying each month Task 1: Your program must display the data in a form in which everything lines up correctly on the screen. Your program should be able to determine the day of the week when the first day of the month starts. To determine the first day of the month for a given year, you need to mark a reference day in history and count from that date. For this, use January 1, 1900, which fell on a Monday. For every year since then, you need to add 365 or 366 days, depending on whether the year was a leap year. Note: A year is a leap year if one of the following conditions hold: The year is divisible by 4 but not divisible by 100, or The year is divisible by 400. Task 2: Give the user the option to print the calendar with one month per row, two months per row, or three months per row.

Explanation / Answer

#include<stdio.h>

#include<conio.h>

#define TRUE 1

#define FALSE 0

int days_month[]={0,31,28,31,30,31,30,31,31,30,31,30,31};//storing day values

char *months[]= //storing month names

{

" ",

" January",

" February",

" March",

" April",

" May",

" June",

" July",

" August",

" September",

" October",

" November",

" December"

};

int giveyear(void) //input for the year is provided

{

int year;

printf("Please enter a year (1900 or more) : ");

scanf("%d", &year);

return year;

}

int findday(int year) //It will determine no. of days in a month

{

int days;

int d1, d2, d3;

d1 = (year - 1.)/ 4.0;

d2 = (year - 1.)/ 100.;

d3 = (year - 1.)/ 400.;

days= (year + d1 - d2 + d3) %7;

return days;

}

int findleapyear(int year)//It will find the leap year and accordingly decide no.of days for feb

{

if(year% 4 == FALSE && year%100 != FALSE || year%400 == FALSE)

{

days_month[2] = 29;

return TRUE;

}

else

{

days_month[2] = 28;

return FALSE;

}

}

void calendar(int year,int days)//It will form the calendar for the year

{

int month, day;

for ( month = 1; month <= 12; month++ )

{

printf("%s", months[month]);

printf(" Sun Mon Tue Wed Thu Fri Sat " );

// It will correct the position for the first date

for ( day = 1; day <= 1 + days * 5; day++ )

{

printf(" ");

}

// It will display all the dates for one month

for ( day = 1; day <= days_month[month]; day++ )

{

printf("%2d", day );

//It will check is day before Sat? Else start next line Sun.

if ( ( day + days ) % 7 > 0 )

printf(" " );

else

printf(" " );

}

//It will set position for the next month

days = ( days + days_month[month] ) % 7;

}

}

int main(void)

{

clrscr();

int year, days, leap_year;

year = giveyear(); //fun call to giveyear

days = findday(year);//fun call to findday

findleapyear(year); //fun call to find leap year

calendar(year, days);//fun call to calendar

printf(" ");

return 0;

}