Program to find the number of days in a given month Last Updated : 04 Mar, 2024 Summarize Comments Improve Suggest changes Share Like Article Like 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: 31Explanation: January has 31 days. Input: month = "february"Output: 28Explanation: 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() Output31 28Time Complexity: O(1)Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article Program to find the number of days in a given month A alphacozeop Follow Improve Article Tags : DSA 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 = 6 min read Program to find the number of days between two dates in PHP In this article, we will see how to get the date difference in the number of days in PHP, along with will also understand the various ways to get the total count of difference in 2 dates & see their implementation through the examples. We have given two dates & our task is to find the number 3 min read Find the number of times every day occurs in a month Given the starting day and the number of days in a month. Find the number of times every day occurs in a month Examples: Input: Number of days in month = 28 First day = Wednesday Output: Monday = 4 Tuesday = 4 Wednesday = 4 Thursday = 4 Friday = 4 Saturday = 4 Sunday = 4 Explanation: In the month of 7 min read Find number of months between two dates in R In this article, we will discuss how to Find the number of months between two dates in the R programming language. Example: Input: Date_1 = 2020/02/21  Date_2 = 2020/03/21 Output: 1 Explanation: In Date_1 and Date_2 have only one difference in month. Here we will use seq() function to get the resu 2 min read How to Get Number of Days in Current Month in PHP? This article will show you how to get the number of days in the current month in PHP. In PHP, you may need to find the number of days in the current month for various purposes, such as generating a calendar or performing date calculations. There are three approaches to get the number of days in the 3 min read Like