![]() |
VOOZH | about |
Your task is to design a menu-driven program that asks the user to input a month number, and the program prints the name of the month.
A menu-driven program is a type of computer program that allows users to interact with it by selecting options from a menu. Instead of typing commands or code, users navigate through a series of menus that present a list of actions or functions they can perform.
Examples:
Enter the number corresponding to a month:
1. January
2. February
3. March
4. April
5. May
6. June
7. July
8. August
9. September
10. October
11. November
12. DecemberEnter the month number: 2
FebruaryEnter Y to continue
Enter any other character to exit
YEnter the month number: 14
Invalid month number. Please enter a number between 1 and 12.Enter Y to continue
Enter any other character to exit
NProgram Terminated
Approach: Using While looping and Switch-Case conditioning
The problem can be solved using a while loop which keeps on asking the user to input the month number, till the user does not press any other key. Inside the while loop, the user enters the month number and if the month number lies in the range 1 to 12, then the corresponding month is printed. As soon as the user presses 'N', the control comes out of the while loop and the program gets terminated.
Below is the implementation of the approach:
Enter the number corresponding to a month:
1. January
2. February
3. March
4. April
5. May
6. June
7. July
8. August
9. September
10. October
11. November
12. December
Enter the month number: 2
February
Enter Y to continue
Enter any other character to exit
Y
Enter the month number: 14
Invalid month number. Please enter a number between 1 and 12.
Enter Y to continue
Enter any other character to exit
N
Program Terminated