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

Write a program to compute the exact number of days between any two dates in his

ID: 3546087 • Letter: W

Question

Write a program to compute the exact number of days between any two dates in history.


This is in C programming, and on UNIX server, so <conio.h> is not a valid headerfile!!!


Enter first date: 10/5/1846

Enter second date: 6/2/2005 There are 57949 days between the two dates.

Note that the year must be entered as four digits since it can be from any century. (For this

assignment you can ignore the fact that different calendars have been used in different periods in

history, and just extend the calendar we currently use forward and backward in time.) The program

should check the input format for each date, including: month between 1 and 12, and date within the

actual number of days for that month. If an input error occurs the program should print an

appropriate message and re-input the date. Use the following rule to compute leap years:

Any year which is a multiple of 4 is a leap year;

unless it

Explanation / Answer

#include <stdio.h>


int leapYear(int y)

{

return (y%400==0 || (y%4==0&&y%100!=0));

}


int maxDay(int y, int m)

{

int mDay;


if (m == 2)

{

if (leapYear(y))

mDay = 29;

else

mDay = 28;

}

else if (m < 8)

{

mDay = (m%2==1)?31:30;

}

else

{

mDay = (m%2==0)?31:30;

}

return mDay;

}


int isvalid(int y, int m, int d)

{

int mDay;

if (m < 1 || m > 12)

return 0;


mDay = maxDay(y, m);

if (d < 1 || d > mDay)

return 0;


return 1;

}


int diffDay(int y1, int m1, int d1, int y2, int m2, int d2)

{

int result;

int i;

int comp = y1 - y2;


if (comp == 0)

comp = m1 - m2;

if (comp == 0)

comp = d1 - d2;

if (comp > 0)

return -1;


result = -d1;

for (i = 1; i < m1; i++)

result = result - maxDay(y1, i);


while (y1 < y2)

{

if (leapYear(y1))

result += 366;

else

result += 365;

y1++;

}

for (i = 1; i < m2; i++)

result = result + maxDay(y2, i);

result += d2;

return result;

}


int main()

{

int y1 = 0, y2 = 0, m1 = 0, m2 = 0, d1 = 0, d2 = 0;

int diff;

printf("Enter first date: ");

scanf("%d/%d/%d", &m1, &d1, &y1);

printf("Enter second date: ");

scanf("%d/%d/%d", &m2, &d2, &y2);


if (!isvalid(y1,m1,d1) || !isvalid(y2,m2,d2))

{

printf("Invalid input!!! ");

}

else

{

diff = diffDay(y1,m1,d1,y2,m2,d2);

if (diff < 0)

printf("Invalid input!!! ");

else

printf("There are %d days between the two dates. ", diff);

}



return 0;

}


Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote