Open In App

Program to count the number of months between given two years

Last Updated : 12 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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 = 2023
Output: 396
Explanation: The number of years between 1991 and 2023 (both inclusive) are: 33. So, total number of months = 396.

Input: startYear = 2010, endYear = 2023
Output: 168
Explanation: The number of years between 2010 and 2023 (both inclusive) are: 14. So total number of months are: 168.

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

We know 1 year has 12 months, so (endYear – startYear + 1) gives number of years between the two given years, where both the starting and ending years are included. So, to find months multiply by 12 as,
1 year = 12 months
n years = n*12 months

Below is the implementation of the above approach:

C++




#include <iostream>
using namespace std;
 
int calculateMonths(int startYear, int endYear)
{
    if (startYear > endYear)
        return -1;
    // Calculate the number of months between the two years
    return (endYear - startYear + 1) * 12;
}
 
int main()
{
    int startYear = 1991, endYear = 2023;
 
    int numberOfMonths
        = calculateMonths(startYear, endYear);
 
    // Invalid Input
    if (numberOfMonths == -1) {
        cout << "Invalid Input" << endl;
    }
    else {
        // Display the result
        cout << "Number of months between " << startYear
             << " and " << endYear
             << "(both inclusive) is: " << numberOfMonths
             << " months." << endl;
    }
 
    return 0;
}


Java




public class MonthCalculator {
 
    // Function to calculate the number of months between two years
    public static int calculateMonths(int startYear, int endYear) {
        // Check for invalid input (start year greater than end year)
        if (startYear > endYear) {
            return -1;
        }
 
        // Calculate the number of months between the two years
        return (endYear - startYear + 1) * 12;
    }
 
    public static void main(String[] args) {
        // Given start and end years
        int startYear = 1991;
        int endYear = 2023;
 
        // Call the function to calculate the number of months
        int numberOfMonths = calculateMonths(startYear, endYear);
 
        // Check for invalid input and display the result
        if (numberOfMonths == -1) {
            System.out.println("Invalid Input");
        } else {
            System.out.println("Number of months between " + startYear + " and " + endYear +
                    " (both inclusive) is: " + numberOfMonths + " months.");
        }
    }
}


Python3




def calculate_months(start_year, end_year):
    # Check for invalid input (start year greater than end year)
    if start_year > end_year:
        return -1
     
    # Calculate the number of months between the two years
    return (end_year - start_year + 1) * 12
 
if __name__ == "__main__":
    # Given start and end years
    start_year = 1991
    end_year = 2023
 
    # Call the function to calculate the number of months
    number_of_months = calculate_months(start_year, end_year)
 
    # Check for invalid input and display the result
    if number_of_months == -1:
        print("Invalid Input")
    else:
        print(f"Number of months between {start_year} and {end_year} (both inclusive) is: {number_of_months} months.")


C#




using System;
 
class Program
{
    static int CalculateMonths(int startYear, int endYear)
    {
        if (startYear > endYear)
            return -1;
         
        // Calculate the number of months between the two years
        return (endYear - startYear + 1) * 12;
    }
 
    static void Main()
    {
        int startYear = 1991, endYear = 2023;
 
        int numberOfMonths = CalculateMonths(startYear, endYear);
 
        // Invalid Input
        if (numberOfMonths == -1)
        {
            Console.WriteLine("Invalid Input");
        }
        else
        {
            // Display the result
            Console.WriteLine($"Number of months between {startYear} and {endYear} (both inclusive) is: {numberOfMonths} months.");
        }
    }
}


Javascript




// JavaScript program with the same function and variable names as the Python code
function calculate_months(start_year, end_year) {
    // Check for invalid input (start year greater than end year)
    if (start_year > end_year) {
        return -1;
    }
     
    // Calculate the number of months between the two years
    return (end_year - start_year + 1) * 12;
}
 
// Given start and end years
const start_year = 1991;
const end_year = 2023;
 
// Call the function to calculate the number of months
const number_of_months = calculate_months(start_year, end_year);
 
// Check for invalid input and display the result
if (number_of_months === -1) {
    console.log("Invalid Input");
} else {
    console.log(`Number of months between ${start_year} and ${end_year} (both inclusive) is: ${number_of_months} months.`);
}


Output

Number of months between 1991 and 2023(both inclusive) is: 396 months.






Time Complexity: O(1)
Auxiliary space: O(1)



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads