VOOZH about

URL: https://www.geeksforgeeks.org/dsa/program-to-count-the-number-of-days-between-two-years/

⇱ Program to count the number of days between two years - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Program to count the number of days between two years

Last Updated : 23 Jul, 2025

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:


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)

Comment
Article Tags:
Article Tags: