Number of days in Month Program in C Language

number-of-days-in-month-program-in-c-language

Program Description:

Write a C Program to display the Number of days in Month. The program should accept the month from the user in the form of a number. i.e 1 for January, 2 for February, 3 for March, and so on until 12 for December.

If the user enters February ( 2), Then the program should ask the year as well. Because the number of days in February depends on the leap year. so we also need the year.

Excepted Output:

Example 1: Input:

Enter a Month (1 for Jan, 2 - Feb, 12 - Dec) : 10

Output:

October month contains 31 days

Example 2: Input:

Enter a Month (1 for Jan, 2 - Feb, ... 12 - Dec) : 4

Output:

April month contains 30 days

Example 3: Input:

Enter a Month (1 for Jan, 2 - Feb, ... 12 - Dec) : 2

As the user enter 2 i.e Feb, We also ask for the year

Enter Year: 2022

Output:

Non Leap Year - February month contains 28 days

Pre-Requisites:

It is recommended to have the basic knowledge of the decision making statements like if else statements and if else ladder, and Logical Operator and switch case.

Please go through the following article to learn more about the above topics.

Number of Days in Month Program using if else ladder Explanation:

  • The Program starts by asking the user for the month number ( 1 – Jan, 2 – Feb, etc) and the Program store the month number in a variable month
  • We know that all months won’t contain the same number of days. Here is the list with the number of days
    • January, March, May, July, August, October, and December – 31 days
    • April, June, September, and November – 30 days
    • February
      • Leap Year – 29 days
      • Non-Leap Year – 28 days
  • Then we use the if else ladder to check the month. If the month is equal to 1, Then it is January, so print the number of days based above list. ( i.e 31 days)
  • If the month is February, Then we might have 28 or 29 days. So we ask the user to enter the Year. Once the year is entered we store it in year variable.
    • Then we calculate if the given number is a Leap Year using the Leap year formula. ((year%400 == 0) || ((year%4 == 0) && (year%100 != 0)))
    • we have already covered the Leap year program in the following article – Please look at it for more details about the Leap year formula – https://sillycodes.com/c-program-to-check-given-year-is-leap/
    • If the year is Leap year, Then we are printing Leap Year - February month contains 29 days
    • If the year is not Leap year, Then we are displaying Non Leap Year - February month contains 28 days on the console.
  • Similarly, we are displaying the number of days for each month.
  • If the user enters an Invalid month number, Then we are displaying the error message – Please enter valid Number for month

Here is the program of the above logic.

Number of Days in Month Program using if else ladder:

Program Output:

Number of Days in Month Program using if else statement and Logical OR Operator:

If you observe the above program, There are many months that have the same number of days, So we can use the logical OR operator to group those months. So Let’s rewrite the above program by using the if-else statements and the logical operator.

Program:

Program Output:

Program to calculate the Number of days in Month using the switch statement:

We can further change the above program and re-write it using the switch statement. The program logic/algorithm remains the same but it becomes more readable compared above two versions.

Here is the program with a switch statement.

Program Output:

As you can see from the above output, We get the same output as the previous two version.

📢 Please make sure to use the break statement appropriately otherwise the subsequent case also executes.

C Tutorials Index

Venkatesh

Hi Guys, I am Venkatesh. I am a programmer and an Open Source enthusiast. I write about programming and technology on this blog.

You may also like...

2 Responses

  1. […] C Program to calculate the Number of days in Month using the switch statement […]

  2. […] C Program to calculate the Number of days in Month using the switch statement […]

Leave a Reply