C program to enter month number and print number of days in month

Write a C program to enter month number between(1-12) and print the number of days in month using if else. How to print number of days in a given month using if else in C programming. Logic to find number of days in a given month in C program.

Example
Input

Enter month number: 1

Output

It contains 31 days.

Required knowledge

Basic C programming, If else

Logic to find number of days in given month number

Below table describes number of days each month contains.

Month Total days
January 31 days
February 28/29 days
March 31 days
April 30 days
May 31 days
June 30 days
July 31 days
August 31 days
September 30 days
October 31 days
November 30 days
December 31 days

There are various ways to code this problem. Let us learn the easiest way to implement this program. Below is the step by step descriptive logic to find number of days in given month.

  1. Input month number from user, store it in some variable say month.
  2. For each month check separately and print the corresponding the number of days in that month, using above table. For example, print 31 days if month == 1 since, January contains 31 days.
  3. Repeat the above step for all 12 months.

Program to print number of days in month

/**
 * C program to print number of days in a month
 */

#include <stdio.h>

int main()
{
    int month;

    /* Input month number from user */
    printf("Enter month number (1-12): ");
    scanf("%d", &month);


    if(month == 1)
    {
        printf("31 days");
    }
    else if(month == 2)
    {
        printf("28 or 29 days");
    }
    else if(month == 3)
    {
        printf("31 days");
    }
    else if(month == 4)
    {
        printf("30 days");
    }
    else if(month == 5)
    {
        printf("31 days");
    }
    else if(month == 6)
    {
        printf("30 days");
    }
    else if(month == 7)
    {
        printf("31 days");
    }
    else if(month == 8)
    {
        printf("31 days");
    }
    else if(month == 9)
    {
        printf("30 days");
    }
    else if(month == 10)
    {
        printf("31 days");
    }
    else if(month == 11)
    {
        printf("30 days");
    }
    else if(month == 12)
    {
        printf("31 days");
    }
    else
    {
        printf("Invalid input! Please enter month number between (1-12).");
    }

    return 0;
}

The above logic is easiest but, it's lengthy and not optimal to implement. You can easily do it using various other approaches. Let me explain the other approach to code this program.

Logic to print number of days in month using OR operator

Before getting on to this logic, I recommend you to have a close look to the above code. You will clearly observe a repeated pattern for some conditions. For months 1, 3, 5, 7, 8, 10 and 12 I am printing same output i.e. 31 days. Similarly for months 4, 6, 9, 11 I am printing same output i.e. 30 days.

If we are performing same task for different if conditions. Then we must try to group them together with logical OR operator. Means print 31 days if month == 1 or month == 3 or month == 5 or month == 7 or month == 8 or month == 10 or month == 12. Repeat the same procedure for 30 days condition.

Program to print days in a month using logical OR operator

/**
 * C program to print number of days in a month using logical operator
 */

#include <stdio.h>

int main()
{
    int month;

    /* Input month number from user */
    printf("Enter month number (1-12): ");
    scanf("%d", &month);


    /* Group all 31 days conditions together using logical or operator */
    if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12)
    {
        printf("31 days");
    }
    else if(month==4 || month==6 || month==9 || month==11)
    {
        // Group all 30 days months together
        printf("30 days");
    }
    else if(month==2)
    {
        printf("28 or 29 days");
    }
    else
    {
        printf("Invalid input! Please enter month number between (1-12).");
    }

    return 0;
}

Now you learnt two approaches to code the above program. Enhance your skills by learning another approach to solve the given program even more efficiently using switch case.

Read more - Program to print number of days in month using switch case.

For this problem you can also use array to optimize the solution. Below is another approach to solve the given problem using array.

Program to print days in a month using logical OR operator


/**
 * C program to print number of days in a month using array
 */
 
#include <stdio.h>
 
int main()
{
    // Constant number of month declarations
    const int MONTHS[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int month;
 
    /* Read month number from user */
    printf("Enter month number (1-12): ");
    scanf("%d", &month);
 
 
    if(month > 0 && month < 13)
    {
	printf("%d days", MONTHS[month - 1]);
    }
    else
    {
        printf("Invalid input! Please enter month number between (1-12).\n");
    }
 
    return 0;
} 

Output

Enter month number (1-12): 1
31 days

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>