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: 31
Explanation: January has 31 days.Input: startMonth = “january”, endMonth = “july”
Output: 212
Explanation: 31 + 28 + 31 + 30 + 31 + 30 + 31 = 212 days are present between January and July.
Approach: To solve the problem, follow the below idea:
Traverse over all the month starting from the first month till we reach the end month. While traversing over the array, sum the number of days and print the final sum.
Below is the implementation of the above approach:
#include <iostream>
using namespace std;
int getMonthNumber(string month) {
if(month == "january")
return 1;
else if(month == "february")
return 2;
else if(month == "march")
return 3;
else if(month == "april")
return 4;
else if(month == "may")
return 5;
else if(month == "june")
return 6;
else if(month == "july")
return 7;
else if(month == "august")
return 8;
else if(month == "september")
return 9;
else if(month == "october")
return 10;
else if(month == "november")
return 11;
else if(month == "december")
return 12;
return -1;
}
void calculateNumberOfDays(string start_month, string end_month) {
int start_month_num = getMonthNumber(start_month);
int end_month_num = getMonthNumber(end_month);
int ans = 0;
if(start_month_num == -1 || end_month_num == -1 || start_month_num > end_month_num) {
cout << "Invalid Input" << endl;
return;
}
int arr[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for(int i = start_month_num; i <= end_month_num; i++) {
ans = ans + arr[i];
}
cout << ans << endl;
}
int main() {
string start_month = "january";
string end_month = "july";
calculateNumberOfDays(start_month, end_month);
return 0;
}
public class Main {
// Function to get the numerical representation of a
// month
public static int getMonthNumber(String month)
{
if (month.equals("january"))
return 1;
else if (month.equals("february"))
return 2;
else if (month.equals("march"))
return 3;
else if (month.equals("april"))
return 4;
else if (month.equals("may"))
return 5;
else if (month.equals("june"))
return 6;
else if (month.equals("july"))
return 7;
else if (month.equals("august"))
return 8;
else if (month.equals("september"))
return 9;
else if (month.equals("october"))
return 10;
else if (month.equals("november"))
return 11;
else if (month.equals("december"))
return 12;
return -1;
}
// Function to calculate the number of days between two
// months
public static void
calculateNumberOfDays(String startMonth,
String endMonth)
{
int startMonthNum = getMonthNumber(startMonth);
int endMonthNum = getMonthNumber(endMonth);
int ans = 0;
if (startMonthNum == -1 || endMonthNum == -1
|| startMonthNum > endMonthNum) {
System.out.println("Invalid Input");
return;
}
int[] daysInMonth = { 0, 31, 28, 31, 30, 31, 30,
31, 31, 30, 31, 30, 31 };
for (int i = startMonthNum; i <= endMonthNum; i++) {
ans += daysInMonth[i];
}
System.out.println(ans);
}
public static void main(String[] args)
{
String startMonth = "january";
String endMonth = "july";
calculateNumberOfDays(startMonth, endMonth);
}
}
def get_month_number(month):
# Dictionary mapping month names to corresponding numbers
months = {
"january": 1,
"february": 2,
"march": 3,
"april": 4,
"may": 5,
"june": 6,
"july": 7,
"august": 8,
"september": 9,
"october": 10,
"november": 11,
"december": 12,
}
# Return the month number using the dictionary, default to -1 if not found
return months.get(month.lower(), -1)
def calculate_number_of_days(start_month, end_month):
# Get the month numbers for start and end months
start_month_num = get_month_number(start_month)
end_month_num = get_month_number(end_month)
ans = 0
# Check for invalid input conditions
if start_month_num == -1 or end_month_num == -1 or start_month_num > end_month_num:
print("Invalid Input")
return
# Number of days in each month (index 0 is not used)
days_in_month = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
# Calculate the total number of days between start and end months
ans = sum(days_in_month[start_month_num : end_month_num + 1])
# Print the result
print(ans)
if __name__ == "__main__":
# Example usage
start_month = "january"
end_month = "july"
calculate_number_of_days(start_month, end_month)
using System;
public class Solution
{
// Function to get the numerical representation of a month
public static int GetMonthNumber(string month)
{
// Convert the month string to lowercase for case-insensitive comparison
switch (month.ToLower())
{
case "january":
return 1;
case "february":
return 2;
case "march":
return 3;
case "april":
return 4;
case "may":
return 5;
case "june":
return 6;
case "july":
return 7;
case "august":
return 8;
case "september":
return 9;
case "october":
return 10;
case "november":
return 11;
case "december":
return 12;
default:
return -1; // Return -1 for invalid input
}
}
// Function to calculate the number of days between two months
public static void CalculateNumberOfDays(string startMonth, string endMonth)
{
// Get the numerical representation of startMonth and endMonth
int startMonthNum = GetMonthNumber(startMonth);
int endMonthNum = GetMonthNumber(endMonth);
int ans = 0;
// Check for invalid input or if startMonth is greater than endMonth
if (startMonthNum == -1 || endMonthNum == -1 || startMonthNum > endMonthNum)
{
Console.WriteLine("Invalid Input");
return;
}
// Array to store the number of days in each month
int[] daysInMonth = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// Iterate over the months from startMonth to endMonth and calculate total days
for (int i = startMonthNum; i <= endMonthNum; i++)
{
ans += daysInMonth[i];
}
// Print the total number of days
Console.WriteLine(ans);
}
// Main method serving as the entry point
public static void Main(string[] args)
{
string startMonth = "january";
string endMonth = "july";
CalculateNumberOfDays(startMonth, endMonth);
}
}
// Function to get the numerical representation of a month
function getMonthNumber(month) {
switch (month.toLowerCase()) { // Convert month to lowercase for case-insensitivity
case "january":
return 1;
case "february":
return 2;
case "march":
return 3;
case "april":
return 4;
case "may":
return 5;
case "june":
return 6;
case "july":
return 7;
case "august":
return 8;
case "september":
return 9;
case "october":
return 10;
case "november":
return 11;
case "december":
return 12;
default:
return -1; // Invalid month
}
}
// Function to calculate the number of days between two months
function calculateNumberOfDays(startMonth, endMonth) {
const startMonthNum = getMonthNumber(startMonth);
const endMonthNum = getMonthNumber(endMonth);
let ans = 0;
// Check for invalid input or out of order months
if (startMonthNum === -1 || endMonthNum === -1 || startMonthNum > endMonthNum) {
console.log("Invalid Input");
return;
}
// Array representing days in each month
const daysInMonth = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
// Calculate total days by summing days of months between start and end
for (let i = startMonthNum; i <= endMonthNum; i++) {
ans += daysInMonth[i];
}
console.log(ans); // Output total days
}
// Main function to test the above functions
function main() {
const startMonth = "january";
const endMonth = "july";
calculateNumberOfDays(startMonth, endMonth);
}
main(); // Execute main function
Output
212
Time Complexity: O(1)
Auxiliary Space: O(1)