Program to count the number of months between given two years
Last Updated :
12 Feb, 2024
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;
return (endYear - startYear + 1) * 12;
}
int main()
{
int startYear = 1991, endYear = 2023;
int numberOfMonths
= calculateMonths(startYear, endYear);
if (numberOfMonths == -1) {
cout << "Invalid Input" << endl;
}
else {
cout << "Number of months between " << startYear
<< " and " << endYear
<< "(both inclusive) is: " << numberOfMonths
<< " months." << endl;
}
return 0;
}
|
Java
public class MonthCalculator {
public static int calculateMonths( int startYear, int endYear) {
if (startYear > endYear) {
return - 1 ;
}
return (endYear - startYear + 1 ) * 12 ;
}
public static void main(String[] args) {
int startYear = 1991 ;
int endYear = 2023 ;
int numberOfMonths = calculateMonths(startYear, endYear);
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):
if start_year > end_year:
return - 1
return (end_year - start_year + 1 ) * 12
if __name__ = = "__main__" :
start_year = 1991
end_year = 2023
number_of_months = calculate_months(start_year, end_year)
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;
return (endYear - startYear + 1) * 12;
}
static void Main()
{
int startYear = 1991, endYear = 2023;
int numberOfMonths = CalculateMonths(startYear, endYear);
if (numberOfMonths == -1)
{
Console.WriteLine( "Invalid Input" );
}
else
{
Console.WriteLine($ "Number of months between {startYear} and {endYear} (both inclusive) is: {numberOfMonths} months." );
}
}
}
|
Javascript
function calculate_months(start_year, end_year) {
if (start_year > end_year) {
return -1;
}
return (end_year - start_year + 1) * 12;
}
const start_year = 1991;
const end_year = 2023;
const number_of_months = calculate_months(start_year, end_year);
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)
Share your thoughts in the comments
Please Login to comment...