Read an integer value. Assume it is the number of a month of the year; print out the name of that month.

Source Code

Brief explanation is provided after the source code.

#include <stdio.h>

int main(int argc, char ** argv) {
    int month;
    printf("Enter month: ");
    scanf("%d", &month);

    switch(month) {
        case 1: printf("January\n");
        break;
        case 2: printf("February\n");
        break;
        case 3: printf("Match\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");

    }
    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

Enter month: 6
June

Brief Explanation

  • The program demands the user to enter a month (as integer)
  • scanf function reads the value entered by the user and assign it to the variable month.
  • Program control jumps to the case label that corresponds to month, and to the default label, if none of the case labels corresponds to month.
  • The break statement is used to jump out of the switch statement.

Add comment


Security code
Refresh