Question: Hi there, I have the bellow code that I would like to understand how exactly it works: int main () { int leapdays, firstday, yr;

Hi there,

I have the bellow code that I would like to understand how exactly it works:

Hi there, I have the bellow code that I would like to

int main ()

{

int leapdays, firstday, yr;

long int normaldays, totaldays;

printf ("Enter year");

scanf("%ld", &yr);

normaldays = (yr - 1) * 365L;

leapdays = (yr - 1)/4 - (yr - 1)/100 + (yr - 1)/400;

totaldays = normaldays + leapdays;

firstday = totaldays %7;

if (firstday ==0)

printf(" Monday");

if (firstday ==1)

printf(" Tuesday");

if (firstday ==2)

printf(" Wednesday");

if (firstday ==3)

printf(" Thursday");

if (firstday ==4)

printf(" Friday");

if (firstday ==5)

printf(" Saturday");

if (firstday ==6)

printf(" Sunday");

}

Can anyone please explain the logic of this code line by line?

Thank you,

int main { int leapdays, firstday, yr; long int normaldays, totaldays; printf ("Enter year"); scanf("%d", &yr); normaldays = (yr - 1) * 365L; How does line works for the code? Could you please provide an example? leapdays = (yr - 1)/4 (yr - 1)/100 + (yr 1)/400; How does line works for the code? Could you please provide an example? totaldays normaldays + leapdays; firstday = totaldays %7; How does line works for the code? Could you please provide an if (firstday ==0) example? printf(" Monday"); if (firstday ==1) printf(" Tuesday"); if (firstday ==2) printf(" Wednesday"); if (firstday ==3) This also I do not understand, could you please privide explanation? printf(" Thursday"); if (firstday ==4) printf(" Friday"); if (firstday ==5) printf(" Saturday"); if (firstday ==6) printf(" Sunday"); }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!