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

Previous Program Next Program

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

Before we actually get into how to do this program. Let us first see number of days each month contains.

Month Total days
January31 days
February28/29 days
March31 days
April30 days
May31 days
June30 days
July31 days
August31 days
September30 days
October31 days
November30 days
December31 days

It is often said in programming that first get correct output then if possible optimize your code. Here I am going to explain two approaches to implement this program. Let us first 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. Read 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 - check if month == 1 then print 31. Since January contains 31 days.
  3. Repeat the above step for all 12 months.

Let us implement our first logic to print number of days in month.

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;

    /* Read 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;
}

As you can see that, the above logic is easiest to implement and get. But is 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. Same for months 4, 6, 9, 11 I am printing same output i.e. 30 days.

There is a concept in programming. If we are performing same task for different if conditions. Then we must 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 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;

    /* Read 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).\n");
    }

    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 cases.

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

Happy coding ;)

You may also like

Previous Program Next Program

Labels: , ,