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

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

Example
Input

Input month number: 3

Output

Total number of days = 31

Required knowledge

Basic programming, Switch case

Logic to find number of days in month

Before we get into logic section for this program. Let us have a look to the table 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

From the above table you can conclude that the month 1, 3, 5, 7, 8, 10, 12 contains 31 days. The months 4, 6, 9, 11 contains 30 days and 2 month contains 28/29 days.

Below is the step by step descriptive logic.

  1. Input month number from user, store it in some variable say month.
  2. Switch the month variable and match with cases.
  3. Print 31 for case 1, 3, 5, 7, 8, 10, 12.
  4. Print 30 for case 4, 6, 9, 11.
  5. Print 28/29 for case 2.
  6. Print invalid input for default case.

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

Observe the above program carefully for a minute. You will notice that I have repeated printf("31 days") and printf("30 days"); for many cases. If a switch undergoes same task for various cases. Then you can remove the break statement and put all the repetitive statements in last repetitive case. As I did in below program, observe it carefully.

Program to find number of days in a 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:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: printf("31 days");
            break;
        case 4:
        case 6:
        case 9:
        case 11: printf("30 days");
            break;
        case 2: printf("28/29 days");
            break;
        default: printf("Invalid input! Please enter month number between 1-12");

    }
    return 0;
}

Note: In the above program for any case in 1, 3, 5, 7, 8, 10, 12 the output will be 31 days and for any case 4, 6, 9, 11 output will be 30 days.

Read more - Program to find number of days in month using if else

Output

Enter month number(1-12): 3
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>