Program to find the number of days in a given month Last Updated : 04 Mar, 2024 Comments Improve Suggest changes 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 Program to calculate the number of odd days in given number of years Given an integer N, the task is to find the number of odd days in the years from 1 to N. Odd Days: Number of odd days refer to those days that are left in a certain year(s) when it's days gets converted into weeks. Say, an ordinary year has 365 days, that is 52 weeks and one odd day. This means, out 7 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 Find number of days between two given dates Given two dates, find total number of days between them. The count of days must be calculated in O(1) time and O(1) auxiliary space. Examples: Input: dt1 = {10, 2, 2014} dt2 = {10, 3, 2015} Output: 393 dt1 represents "10-Feb-2014" and dt2 represents "10-Mar-2015" The difference is 365 + 28 Input: dt 11 min read How to find the first Monday of a given month using NumPy? To find the first Monday of a given month, we are going to use the numpy module i.e the numpy.busday_offset() method in numpy module. Syntax: np.busday_offset('date', 0, roll='forward', weekmask='Mon') Parameters: date: The array of dates to process. offsets: The array of offsets, which is broadcast 2 min read How to to Get Number of Days in Current Month in Moment.JS? While working with dates in JavaScript, sometimes we need to find the number of days in the current month. For example, February has 28 or 29 days depending on whether it's a leap year or not, while other months have 30 or 31 days. Moment.js is a powerful JavaScript library that makes handling dates 2 min read Find the day number in the current year for the given date Given string str which represents a date formatted as YYYY-MM-DD, the task is to find the day number for the current year. For example, 1st January is the 1st day of the year, 2nd January is the 2nd day of the year, 1st February is the 32nd day of the year and so on. Examples: Input: str = "2019-01- 6 min read Like