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;
}
We include the
stdio.h
header file, which contains the necessary functions for input and output operations in C.The
main()
function is the entry point of the program. It is where the execution of the program starts.We declare an integer variable
month
to store the user input for the month number.We use the
printf()
function to display a message asking the user to enter the month number.We use the
scanf()
function to read the user input for the month number and store it in themonth
variable.We use a
switch
statement to check the value of themonth
variable.Each
case
in theswitch
statement corresponds to a month number from 1 to 12.If the value of
month
matches acase
, the corresponding month name is printed using theprintf()
function.The
break
statement is used to exit theswitch
statement once a match is found. Without thebreak
statement, execution will continue to the nextcase
and output multiple month names.If the value of
month
does not match anycase
, thedefault
case is executed, which prints an "Invalid month number" message.The program ends with the
return 0;
statement, which indicates successful execution of the program.