I need to use the following formula to calculate the difference between two date
ID: 3644430 • Letter: I
Question
I need to use the following formula to calculate the difference between two dates using Structures in C.N = 1461 x f(year, month) / 4 + 153 g(month /5 + day
where
f(year,month) = year-1 if month <=2
year otherwise
g(month) month + 13 if month <=2
month + 1 otherwise
This formula only works for dates after March 1, 1900 (1 must be added for dates form March 1, 1800 to February 28, 1900 and 2 must be added for dates between March 1, 1700 to February 28, 1800
I have to write a program that permits the user to type in two dates and then calculates the number of elapsed days.
Thank you
Explanation / Answer
Please rate...
Ignore the previous one...
Take this one...
#include <stdio.h>
void main () {
struct date {
int month;
int day;
int year;
};
struct date d1, d2;
int n,n1,n2;
int f(int,int);
int g(int);
printf ("Enter date 1 (MM/DD/YYYY): ");
scanf ("%d/%d/%d", &d1.month, &d1.day, &d1.year);
printf (" Enter date 2 (MM/DD/YYYY): ");
scanf ("%d/%d/%d", &d2.month, &d2.day, &d2.year);
n1=(1462*(f(d1.year,d1.month)/4))+(((153*g(d1.month))/5)+d1.day);
n2=(1462*(f(d2.year,d2.month)/4))+(((153*g(d2.month))/5)+d2.day);
if(d1.year>=1801 && d1.year<=1899)n1=n1+1;
else if(d1.year==1800 && d1.month>=3)n1=n1+1;
else if(d1.year==1900)
{
if(d1.month==1)n1=n1+1;
if(d1.month==2 &&d1.day<=28)n1=n1+1;
}
if(d1.year>=1701 && d1.year<=1799)n1=n1+2;
else if(d1.year==1700 && d1.month>=3)n1=n1+2;
else if(d1.year==1800)
{
if(d1.month==1)n1=n1+2;
if(d1.month==2 &&d1.day<=28)n1=n1+2;
}
if(d2.year>=1801 && d2.year<=1899)n2=n2+1;
else if(d2.year==1800 && d2.month>=3)n2=n2+1;
else if(d2.year==1900)
{
if(d2.month==1)n2=n2+1;
if(d2.month==2 &&d2.day<=28)n2=n2+1;
}
if(d2.year>=1701 && d2.year<=1799)n2=n2+2;
else if(d2.year==1700 && d2.month>=3)n2=n2+2;
else if(d2.year==1800)
{
if(d2.month==1)n2=n2+2;
if(d2.month==2 &&d2.day<=28)n2=n2+2;
}
n=n1>n2?n1-n2:n2-n1;
printf(" The number of days are: %d",n);
}
int f(int year,int month)
{
if(month<=2)return (year-1);
else return year;
}
int g(int month)
{
if(month<=2)return (month+13);
else return (month+1);
}