This calculator calculates the duration, which is the number of days between two
ID: 3575476 • Letter: T
Question
This calculator calculates the duration, which is the number of days between two dates. Pay attention to that days in months are different.
Month
January
February
March
April
May
June
July
August
September
October
November
December
The following algorithm can be used to determines whether a year is a leap year.
if (year is not divisible by 4) then (it is a common year)
else if (year is not divisible by 100) then (it is a leap year)
else if (year is not divisible by 400) then (it is a common year)
else (it is a leap year)
Write a Date class, and use member function dateDiff to caculate the duration.
Sample Input
1 12 2016
3 12 2016
Sample Output
2
Tips
If the first date is later than the second date, output a negative number
I suggest you using some websites to verify the accuracy of your program. Google "date difference calculator".
www.timeanddate.com. The algorithm on this website got some bugs, so use other websites to double check if you found your program got a different answer with the result on www.timeanddate.com
Month
Days in MonthJanuary
31February
28 (29 in leap years)March
31April
30May
31June
30July
31August
31September
30October
31November
30December
31Explanation / Answer
Solution :-
#include<stdio.h>
int finddays(int d1,int d2,int m1,int m2,int y1,int y2);
int tmonths(int d,int y);
int month[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int tdays=0;
int main()
{
int dd1,mm1,yy1,dd2,mm2,yy2,days;
printf("Enter two dates (format- dd mm yyyy) ");
scanf("%d%d%d",&dd1,&mm1,&yy1);
scanf("%d%d%d",&dd2,&mm2,&yy2);
days=finddays(dd1,dd2,mm1,mm2,yy1,yy2);
days=tdays+(dd2-1);
printf("The Total Daya are :-%d ",days);
return 0;
}
int finddays(int d1,int d2,int m1,int m2,int y1,int y2)
{
int i;
for(i=y1;i<y2;i++)
{
if(i%4==0)
tdays=tdays+366;
else
tdays=tdays+365;
}
tdays=tdays-tmonths(y1,m1);
tdays=tdays-(d1-1);
tdays=tdays+tmonths(y2,m2);
return tdays;
}
int tmonths(int y,int m)
{
int i,ttdays=0;
for(i=0;i<m-1;i++)
{
if(i==1)
{
if(y%4==0)
ttdays=ttdays+29;
else
ttdays=ttdays+28;
}
else
ttdays=ttdays+month[i];
}
return ttdays;
}
SampleOutput 1:-
Enter two dates (format- dd mm yyyy)
1 1 2016
1 1 2017
The Total Daya are :-366
SampleOutput 2:-
Enter two dates (format- dd mm yyyy)
8 4 1995
12 11 2015
The Total Daya are :-7523
Note :- it excludes end day;
Thank you!