C program to find number of days in month

Write a C program to enter month number between(1-12) and print 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 month in C program.

Example
Input

Enter month number: 1

Output

It contains 31 days.

Required knowledge

Basic C programming, Relational operators, If else

Logic to find number of days in a month

Total days in a months is given by below table.

MonthTotal days
January, March, May, July, August, October, December31 days
February28/29 days
April, June, September, November30 days

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 corresponding 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;
}

Logic to print number of days in month using logical OR operator

The above logic is simple and easy to code. But it’s lengthy and not optimal to implement. In the above solution we are performing same task for multiple conditions i.e. print 31 days for month 1, 3, 5, 7, 8, 10, 12 and print 30 days for month 4, 6, 9, 11.

To perform single task on multiple condition, we use logical OR operator ||. Logical OR operator groups multiple conditions and evaluate true if any of the condition is true.

You can group all conditions for 31 days together as if(month==1 || month==3 || month==5 || month==7 || month==8 || month==10 || month==12).

Similarly group all conditions for 30 days as if(month==4 || month==6 || month==9 || month==11).

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;
}

We saw two approaches to code this problem. As I always say if…else is not recommended to use for fixed value condition checking. We must use switch…case statement to perform action based on fixed choices (constant month number 1-12 in this case).

Learn – Program to print number of days in month using switch case.

For this problem you can also define constant number of days in array to optimize the solution. Below is another approach to solve the given problem using array. But to understand the below approach you must have at least basic knowledge of arrays.

Program to print days in a month using array

/**
 * 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;
 
    /* Input month number from user */
    printf("Enter month number (1-12): ");
    scanf("%d", &month);
 
 
    if(month >= 1 && month <= 12)
    {
        /* Print number of days */
	printf("%d days", MONTHS[month - 1]);
    }
    else
    {
        printf("Invalid input! Please enter month number between (1-12).");
    }
 
    return 0;
}

Output

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

Happy coding 😉