C Program for find difference between two date
Code
#include<conio.h>
void main()
{
int date1,date2,month1,month2,year1,year2,leap_day,i;
long int year_differnt,totalday1=0,totalday2=0,total_day;
int day[]={31,0,31,30,31,30,31,31,30,31,30,31};
printf("enter the 1st date month and year");
scanf("%d%d%d",&date1,&month1,&year1);
printf("enter the 2nd date month and year");
scanf("%d%d%d",&date2,&month2,&year2);
for(i=month1;i<12;i++)
{
if(i==2)
{
if((year1%4==0)||((year1%100==0)&(year1%400)))
totalday1=totalday1+29;
else
totalday1=totalday1+28;
}
else
totalday1=totalday1+day[i];
}
totalday1=totalday1+date1;
for(i=month2;i<12;i++)
{
if(i==2)
{
if((year1%4==0)||((year1%100==0)&(year1%400)))
totalday2=totalday2+29;
else
totalday2=totalday2+28;
}
else
totalday2=totalday2+day[i];
}
totalday2=totalday2+date2;
if(year1<=year2)
year_differnt=year2-year1;
else
year_differnt=year1-year2;
leap_day= year_differnt/4;
total_day= (year_differnt*365)+leap_day;
if(totalday1>totalday2)
total_day= total_day+(totalday1-totalday2);
else
total_day= total_day+(totalday2-totalday1);
printf("\n%d-%d-%d",date1,month1,year1);
printf("\n%d-%d-%d",date2,month2,year2);
printf("\n difference between following date is :%ld day",total_day);
getch();
}
Output
No comments:
Post a Comment