C program to input the month number and output the month name using switch statement

#include <stdio.h>

int main() {
    int month;

    printf("Enter the month number: ");
    scanf("%d", &month);

    switch (month) {
        case 1:
            printf("January\n");
            break;
        case 2:
            printf("February\n");
            break;
        case 3:
            printf("March\n");
            break;
        case 4:
            printf("April\n");
            break;
        case 5:
            printf("May\n");
            break;
        case 6:
            printf("June\n");
            break;
        case 7:
            printf("July\n");
            break;
        case 8:
            printf("August\n");
            break;
        case 9:
            printf("September\n");
            break;
        case 10:
            printf("October\n");
            break;
        case 11:
            printf("November\n");
            break;
        case 12:
            printf("December\n");
            break;
        default:
            printf("Invalid month number\n");
            break;
    }

    return 0;
}
  1. We include the stdio.h header file, which contains the necessary functions for input and output operations in C.

  2. The main() function is the entry point of the program. It is where the execution of the program starts.

  3. We declare an integer variable month to store the user input for the month number.

  4. We use the printf() function to display a message asking the user to enter the month number.

  5. We use the scanf() function to read the user input for the month number and store it in the month variable.

  6. We use a switch statement to check the value of the month variable.

  7. Each case in the switch statement corresponds to a month number from 1 to 12.

  8. If the value of month matches a case, the corresponding month name is printed using the printf() function.

  9. The break statement is used to exit the switch statement once a match is found. Without the break statement, execution will continue to the next case and output multiple month names.

  10. If the value of month does not match any case, the default case is executed, which prints an "Invalid month number" message.

  11. The program ends with the return 0; statement, which indicates successful execution of the program.