Open In App

Program to count the number of days between two years

Last Updated : 03 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 <iostream>;
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)



Similar Reads

Program to count number of days between two given months
Write a program to count the number of days between two given months (including start as well as end month). Assume the number of days in February as 28. Examples: Input: startMonth = "january", endMonth ="january"Output: 31Explanation: January has 31 days. Input: startMonth = "january", endMonth = "july"Output: 212Explanation: 31 + 28 + 31 + 30 +
6 min read
Program to count the number of months between given two years
Given a start year and end year. Write a program to count number of months between given two years (both inclusive). Examples: Input: startYear = 1991, endYear = 2023Output: 396Explanation: The number of years between 1991 and 2023 (both inclusive) are: 33. So, total number of months = 396. Input: startYear = 2010, endYear = 2023Output: 168Explanat
4 min read
Program to print the number of days in a given year
Write a program to print the number of days in a given year. Examples: Input: 2023Output: 365Explanation: The year 2023 is not a leap year, therefore the number of days are 365. Input: 2024Output: 366Explanation: The year 2024 is a leap year, therefore the number of days are 366. Approach: To solve the problem, follow the below idea: To determine t
4 min read
Program to find the number of days in a given month
Write a program to find the number of days in a given month. We are given name of a month in lowercase letters, so print the number of days in the month. Note: For February, consider the number of days as 28. Examples: Input: month = "january"Output: 31Explanation: January has 31 days. Input: month = "february"Output: 28Explanation: February has 28
3 min read
Program to convert days to weeks
Write a program to convert a given number of days to weeks. Examples: Input: 14 daysOutput: 2 weeks Input: 30 daysOutput: 4 weeks and 2 days Approach: To solve the problem, follow the below idea: Divide the given number of days by 7 to get the number of weeks. The remainder will be the remaining days. Step-by-step algorithm: Read the number of days
2 min read
Program to convert weeks to days
Given no of weeks. Write a program to convert weeks to days. Examples: Input: weeks = 4Output: 28 days Input: weeks = 2Output: 14 days Approach: To solve the problem, follow the below idea: Since we know a week has 7 days. So, to find no of days in a week, we multiply weeks by 7. 1 week = 7 daysn weeks = n*7 days Below is the implementation of the
3 min read
Program to generate a random two-digit number
Write a program to generate a random two-digit number. Example 1: 73Example 2: 26 Approach: To solve the problem, follow the below idea: We can generate a random two-digit number by generating a random integer first and then modulo it by 90. Now, after modulo 90 the remainder can be in the range 0 to 89, so in order to have the remainder as a two-d
2 min read
Program to count the number of consonants in a word
Write a program to count the number of consonants in a given word. Consonants are the letters of the English alphabet excluding vowels ('a', 'e', 'i', 'o', 'u'). Examples: Input: "programming"Output: 8 Explanation: The eight consonants are: 'p', 'r', 'g', 'r', 'm', 'm', 'n', 'g') Input: "hello"Output: 3 Explanation: There are three consonants: 'h',
5 min read
Program to count the number of characters in a word
Write a program to count the number of characters in a word. Examples: Input: Word: "programming"Output: 11 Input: Word: "hello"Output: 5 Approach: To solve the problem, follow the below idea: To count the number of characters in a word, maintain a variable to count all the characters. Iterate through each character in the word and increment the co
2 min read
Program to count the number of vowels in a word
Write a program to count the number of vowels in a given word. Vowels include 'a', 'e', 'i', 'o', and 'u' (both uppercase and lowercase). The program should output the count of vowels in the word. Examples: Input: "programming"Output: 3Explanation: The word "programming" has three vowels ('o', 'a', 'i'). Input: "HELLO"Output: 2Explanation: The word
3 min read
Article Tags :