Program to count the number of days between two years

Last Updated : 03 Jan, 2024

Given two years. Write a program to count the number of days between the two years.

Examples:

Input: startYear = 1990, endYear = 2001
Output: 4383
Explanation: Years 1992, 1996 and 2000 are leap year and all 9 others are non-leap years. So, total number of days are: 366 * 3 + 365 * 9 = 4383

Input: startYear = 1890, endYear = 1998
Output: 39811

Approach: To solve the problem, follow the below idea:

The Approach is to iterate from the starting year to the ending year, and for each of them check if the given year is a leap year or not, as leap year has 366 days while non leap year has 365 days.

Step-by-step approach:

• Iterate from startYear to endYear.
• For Each iteration, check for leap year or non-leap year.
• If year is a leap year add 366 days in the noOfDays var.
• Else add 365 days in the noOfDays.

Below is the implementation of the above approach:

C++

 #include ; using namespace std;   bool isLeapYear(int year) {     // Leap year is divisible by 4, but not divisible by 100     // unless divisible by 400     return (year % 4 == 0 && year % 100 != 0)            || (year % 400 == 0); }   int daysInYear(int year) {     if (isLeapYear(year))         return 366;     return 365; }   int main() {     int startYear = 1890, endYear = 1998;       // Calculate the number of days between the two years     int numberOfDays = 0;     for (int year = startYear; year <= endYear; ++year) {         numberOfDays += daysInYear(year);     }       // Display the result     cout << "Number of days between " << startYear          << " and " << endYear << " is: " << numberOfDays          << " days." << endl;       return 0; }

Java

 public class NumberOfDaysCalculator {     public static boolean isLeapYear(int year)     {         // Leap year is divisible by 4, but not divisible by         // 100 unless divisible by 400         return (year % 4 == 0 && year % 100 != 0)             || (year % 400 == 0);     }       public static int daysInYear(int year)     {         return isLeapYear(year) ? 366 : 365;     }       public static void main(String[] args)     {         int startYear = 1890, endYear = 1998;           // Calculate the number of days between the two         // years         int numberOfDays = 0;         for (int year = startYear; year <= endYear;              ++year) {             numberOfDays += daysInYear(year);         }           // Display the result         System.out.println(             "Number of days between " + startYear + " and "             + endYear + " is: " + numberOfDays + " days.");     } }

Python3

 def is_leap_year(year):     # Leap year is divisible by 4, but not divisible by 100 unless divisible by 400     return (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0)     def days_in_year(year):     return 366 if is_leap_year(year) else 365     def main():     start_year, end_year = 1890, 1998       # Calculate the number of days between the two years     number_of_days = sum(days_in_year(year)                          for year in range(start_year, end_year + 1))       # Display the result     print(         f"Number of days between {start_year} and {end_year} is: {number_of_days} days.")     if __name__ == "__main__":     main()

C#

 using System;   class Program {     static bool IsLeapYear(int year)     {         // Leap year is divisible by 4, but not divisible by         // 100 unless divisible by 400         return (year % 4 == 0 && year % 100 != 0)             || (year % 400 == 0);     }       static int DaysInYear(int year)     {         return IsLeapYear(year) ? 366 : 365;     }       static void Main()     {         int startYear = 1890, endYear = 1998;           // Calculate the number of days between the two         // years         int numberOfDays = 0;         for (int year = startYear; year <= endYear;              ++year) {             numberOfDays += DaysInYear(year);         }           // Display the result         Console.WriteLine(             \$"Number of days between {startYear} and {endYear} is: {numberOfDays} days.");     } }

Javascript

 function isLeapYear(year) {     // Leap year is divisible by 4, but not divisible by 100 unless divisible by 400     return (year % 4 === 0 && year % 100 !== 0) || (year % 400 === 0); }   function daysInYear(year) {     return isLeapYear(year) ? 366 : 365; }   function main() {     let startYear = 1890, endYear = 1998;       // Calculate the number of days between the two years     let numberOfDays = 0;     for (let year = startYear; year <= endYear; ++year) {         numberOfDays += daysInYear(year);     }       // Display the result     console.log(`Number of days between \${startYear} and \${endYear} is: \${numberOfDays} days.`); }   main();

Output

Number of days between 1890 and 1998 is: 39811 days.

Time Complexity: O(N), where N is the number of years between start and end year.
Auxiliary space: O(1)

Article Tags :