Open In App

Program to find the number of days in a given month

Last Updated : 04 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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: 31
Explanation: January has 31 days.

Input: month = “february”
Output: 28
Explanation: February has 28 days.

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

Input the name of the month and print the corresponding number of days using if-else statement.

Step-by-step algorithm:

  • If the month is February, return 28.
  • If the month is April, June, September or November, return 30.
  • Otherwise, return 31.

Below is the implementation of the approach:

C++




#include <iostream>
using namespace std;
 
int getNumberOfDays(string month)
{
    if (month == "february")
        return 28;
    if (month == "april" || month == "june"
        || month == "september" || month == "november")
        return 30;
    return 31;
}
 
int main()
{
 
    string month1 = "january";
    string month2 = "february";
 
    cout << getNumberOfDays(month1) << endl;
    cout << getNumberOfDays(month2) << endl;
    return 0;
}


Java




public class MonthDays {
 
    public static int getNumberOfDays(String month) {
        if (month.equals("february"))
            return 28;
        if (month.equals("april") || month.equals("june")
            || month.equals("september") || month.equals("november"))
            return 30;
        return 31;
    }
 
    public static void main(String[] args) {
        String month1 = "january";
        String month2 = "february";
 
        System.out.println(getNumberOfDays(month1));
        System.out.println(getNumberOfDays(month2));
    }
}


C#




using System;
 
public class Program
{
    // Function to get the number of days in a given month
    public static int GetNumberOfDays(string month)
    {
        if (month == "february")
            return 28;
        if (Array.Exists(new string[] { "april", "june", "september", "november" }, m => m == month))
            return 30;
        return 31;
    }
 
    // Main function
    public static void Main(string[] args)
    {
        string month1 = "january";
        string month2 = "february";
 
        // Display the number of days for each month
        Console.WriteLine(GetNumberOfDays(month1));
        Console.WriteLine(GetNumberOfDays(month2));
    }
}


Javascript




function getNumberOfDays(month) {
    if (month === "february")
        return 28;
    if (month === "april" || month === "june"
        || month === "september" || month === "november")
        return 30;
    return 31;
}
 
// Driver code
    const month1 = "january";
    const month2 = "february";
 
    console.log(getNumberOfDays(month1));
    console.log(getNumberOfDays(month2));
 
 
//This code is contributed by Adasrh.


Python3




def get_number_of_days(month):
    # Function to get the number of days in a given month
    if month == "february":
        return 28
    if month in ["april", "june", "september", "november"]:
        return 30
    return 31
 
# Main function
def main():
    month1 = "january"
    month2 = "february"
 
    # Display the number of days for each month
    print(get_number_of_days(month1))
    print(get_number_of_days(month2))
 
if __name__ == "__main__":
    main()


Output

31
28




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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads