C program to print number of days in a month using switch case

Write a C program to input month number and print total number of days in month using switch...case. C program to find total number of days in a month using switch...case. Logic to print number of days in a month using switch...case in C programming.

Example
Input

Input month number: 3

Output

Total number of days = 31

Required knowledge

Basic C programming, Switch case statement

Logic to print number of days in month using switch...case

Total days in a month 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 print number of days in a month using switch...case.

  1. Input month number from user. Store it in some variable say month.
  2. Switch the value of month i.e. switch(month) and match with cases.
  3. There can be 12 possible values (choices) of month i.e. from 1 to 12. Hence, write 12 cases inside switch and one default case as else block.
  4. Print 31 for case 1, 3, 5, 7, 8, 10, 12.
  5. Print 30 for case 4, 6, 9, 11.
  6. Print 28/29 for case 2.
  7. Print invalid input for default case.

Learn – Program to print number of days in month using if…else.

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

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

#include <stdio.h>

int main()
{
    int month;

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

    switch(month)
    {
        case 1: 
            printf("31 days");
            break;
        case 2: 
            printf("28/29 days");
            break;
        case 3: 
            printf("31 days");
            break;
        case 4: 
            printf("30 days");
            break;
        case 5: 
            printf("31 days");
            break;
        case 6: 
            printf("30 days");
            break;
        case 7: 
            printf("31 days");
            break;
        case 8: 
            printf("31 days");
            break;
        case 9: 
            printf("30 days");
            break;
        case 10: 
            printf("31 days");
            break;
        case 11: 
            printf("30 days");
            break;
        case 12: 
            printf("31 days");
            break;
        default: 
            printf("Invalid input! Please enter month number between 1-12");

    }

    return 0;
}

Logic to print number of days in a month using switch...case – best approach

Observe the above program carefully for a moment. In the above program we are performing same action on multiple cases. We are printing “31 days” and “30 days” for multiple cases.

Instead of repeating same action on multiple cases, you can group similar cases together and perform single action on group of cases. To group, arrange all similar cases one after other and remove break statement from all cases other than last case. For example to group cases for “31 days” use

switch(month)
{
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        printf("31 days.");
        break;
}

If the above code, if program control switches to any of the case in 1, 3, 5, 7, 8, 10 or 12, it executes all the below statement till break statement is found. So for any cases in 1, 3, 5, 7, 8, 10 or 12 it prints “31 days.”

Similarly define all other cases.

Program to find number of days in a month using switch...case – best approach

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

#include <stdio.h>

int main()
{
    int month;

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

    switch(month)
    {
        /* Group all 31 days cases together */
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: 
            printf("31 days");
            break;

        /* Group all 30 days cases together */
        case 4:
        case 6:
        case 9:
        case 11: 
            printf("30 days");
            break;

        /* Remaining case */
        case 2: 
            printf("28/29 days");
            break;

        default: 
            printf("Invalid input! Please enter month number between 1-12");
    }

    return 0;
}

Output

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

Happy coding 😉