Input number and print the corresponding month name using switch case in C++
switch case - Question 1
In this question, we will see how to input an integer between 1 to 12 and print the corresponding name of month in C++ programming using the switch case statement. To know more about switch case statement click on the switch case statement lesson.
Q1) Write a program in C++ to input an integer between 1 to 12 and print the corresponding name of month using the switch case statement.
Program
#include <iostream>
#include <conio.h>
using namespace std;
int main()
{
int n;
cout<<"Enter a number between 1 to 12 : ";
cin>>n;
switch(n)
{
case 1:
cout<<"January";
break;
case 2:
cout<<"February";
break;
case 3:
cout<<"March";
break;
case 4:
cout<<"April";
break;
case 5:
cout<<"May";
break;
case 6:
cout<<"June";
break;
case 7:
cout<<"July";
break;
case 8:
cout<<"August";
break;
case 9:
cout<<"September";
break;
case 10:
cout<<"October";
break;
case 11:
cout<<"November";
break;
case 12:
cout<<"December";
break;
default:
cout<<"Invalid Input";
}
return 0;
}
Output
Enter a number between 1 to 12 : 8 August