#include<iostream>
using namespace std;
int countLeapYears(int years,int month)
{
// Check if feburary has passed in that date
// if yes, the year can taken to calculate
//leap yeara
if (month <= 2)
years--;
// A leap year is a multiple of 4,
return years / 4 - years / 100 + years / 400;
}
// To store number of days in each month.
const int monthDays[12] = {31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31};
// This function returns number of days between given
// date and 14th august 1947
int num_indep_days(int day, int month, int year)
{
//intializing no. of days using year and day
long int d2 = year*365 + day;
// Add days of months in given date
for (int i=0; i<month - 1; i++)
d2 += monthDays[i];
// Since every leap year is of 366 days,
// Add one day for every leap year
d2 += countLeapYears(year,month);
//similarly count the days of 14th august 1947
long int d1 = (1947*365) + 14;
for (int i=0; i<7; i++)
d1 += monthDays[i];
d1 += countLeapYears(1947,8);
// return difference between two counts
return (d2 - d1);
}
int main()
{
//intializing the variables of date
int day=15;
int month=6;
int year=2020;
//calling the num_indep_days
cout << "Difference between two dates is " << num_indep_days(day,month,year) << " days.";
return 0;
}
Comments
Leave a comment