C program print total number of days in a given month using switch Statement

C program print total number of days in a given month using switch Statement

In this tutorial, you will learn how to write a program to print the total number of days in a given month. To achieve this we will use the switch statement.

Our problem statement :

Every month in a calendar year has a different number of days. We are taking a user input between 1 to 12 and showing a fixed output based on the entered value.

Example :

Input: 5

Output: May has 31 days.

When the user input matches the correct cases of the switch statement it executes that block. Input beyond the cases goes into the default block.

#include <stdio.h>
int main() {
    int month;
    printf("Enter the month number (1-12): ");
    scanf("%d", &month);
    switch (month) {
        case 1:
            printf("January has 31 days.\n");
            break;
        case 2:
            printf("February has 28 or 29 days depending on whether it's a leap year.\n");
            break;
        case 3:
            printf("March has 31 days.\n");
            break;
        case 4:
            printf("April has 30 days.\n");
            break;
        case 5:
            printf("May has 31 days.\n");
            break;
        case 6:
            printf("June has 30 days.\n");
            break;
        case 7:
            printf("July has 31 days.\n");
            break;
        case 8:
            printf("August has 31 days.\n");
            break;
        case 9:
            printf("September has 30 days.\n");
            break;
        case 10:
            printf("October has 31 days.\n");
            break;
        case 11:
            printf("November has 30 days.\n");
            break;
        case 12:
            printf("December has 31 days.\n");
            break;
        default:
            printf("Invalid input. Please enter a number between 1 and 12.\n");
            break;
    }
    return 0;
}

Output:

Enter the month number (1-12): 6
June has 30 days.