// Input: Two Dates 
// Output: # of days between the two dates 
#include <iostream.h>

int DaysInMonth(int, int); 
int LeapYear(int); 
int DaysInYear(int); 

main() { 
	int Day1, Month1, Year1; 
	int Day2, Month2, Year2; 
	int DaysElapsed; 

	// 1. Input the two dates 
	cout << "Enter the day1:"; cin >> Day1; 
cout << "Enter month1:"; cin >> Month1;
cout << "Enter Year1:"; cin >> Year1;
cout << "Enter the day2:"; cin >> Day2;
cout << "Enter month2:"; cin >> Month2;
cout << "Enter Year2:"; cin >> Year2; // 2. Initialize DaysElapsed to 0 DaysElapsed = 0;
// Compute the # of days elapsed
// If the two dates are in a different year
if (Year2 > Year1) {
// Add all the days from Day1 to Dec 31 or Year1
// Add all the remaining days in Month1
DaysElapsed += DaysInMonth(Month1, Year1) - Day1; // Add all the days in remaining months until Dec
for (int m=Month1+1; m <= 12; m++)
DaysElapsed += DaysInMonth(m, Year1); // Add all the days in each year between Year1 and Year2
for (int y=Year1+1; y < Year2; y++)
DaysElapsed += DaysInYear(y); // Add all the days from Jan1 of Year2 until Day2
// Add all the days in months from Jan until previous month
for (int m=1; m < Month2; m++)
DaysElapsed += DaysInMonth(m, Year2); // Add Day2 DaysElapsed += Day2; }
// 3. Output the two dates and days elapsed
cout << "Date 1: " << Month1 << "/" << Day1 << "/" << Year1 << endl;
cout << "Date 2: " << Month2 << "/" << Day2 << "/" << Year2 << endl;
cout << "# of days between the two dates is " << DaysElapsed << endl;
} int DaysInYear(int y) {
if (LeapYear(y)) return 366;
else
return 365;
} int LeapYear(int y) {
// determines if y is a leap year
return ((((y % 4) == 0) && ((y % 100) != 0)) || ((y % 400) == 0));
} int DaysInMonth(int m, int y) {
// Computes the number of days in month m in year y
switch (m) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12: return 31;
case 4:
case 6:
case 9:
case 11: return 30;
case 2: if (LeapYear(y))
return 29;
else
return 28;
} return 0;
}