Open In App

Program to print the name of month using the month number | Menu-Driven

Last Updated : 21 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

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. 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

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:

C++




#include <iostream>
using namespace std;
 
int main()
{
    int monthNumber;
 
    // Display the menu
    cout << "Enter the number corresponding to a month: \n";
    cout << "1. January\n";
    cout << "2. February\n";
    cout << "3. March\n";
    cout << "4. April\n";
    cout << "5. May\n";
    cout << "6. June\n";
    cout << "7. July\n";
    cout << "8. August\n";
    cout << "9. September\n";
    cout << "10. October\n";
    cout << "11. November\n";
    cout << "12. December\n";
    while (true) { // Get user input
        cout << "Enter the month number: ";
        cin >> monthNumber;
 
        // Check the input and output the corresponding
        // month
        switch (monthNumber) {
        case 1:
            cout << "January\n";
            break;
        case 2:
            cout << "February\n";
            break;
        case 3:
            cout << "March\n";
            break;
        case 4:
            cout << "April\n";
            break;
        case 5:
            cout << "May\n";
            break;
        case 6:
            cout << "June\n";
            break;
        case 7:
            cout << "July\n";
            break;
        case 8:
            cout << "August\n";
            break;
        case 9:
            cout << "September\n";
            break;
        case 10:
            cout << "October\n";
            break;
        case 11:
            cout << "November\n";
            break;
        case 12:
            cout << "December\n";
            break;
        default:
            cout << "Invalid month number. Please enter a "
                    "number between 1 and 12.\n";
        }
        char choice;
        cout << "Enter Y to continue" << endl;
        cout << "Enter any other character to exit" << endl;
        cin >> choice;
        if (choice != 'Y')
            break;
    }
      cout << "Program Terminated" << endl;
 
    return 0;
}


Java




import java.util.NoSuchElementException;
import java.util.Scanner;
 
public class Main {
    // Creating a Scanner object for user input
    static Scanner scanner = new Scanner(System.in);
 
    // Function to display the menu of months
    static void displayMenu()
    {
        System.out.println(
            "Enter the number corresponding to a month: ");
        System.out.println("1. January");
        System.out.println("2. February");
        System.out.println("3. March");
        System.out.println("4. April");
        System.out.println("5. May");
        System.out.println("6. June");
        System.out.println("7. July");
        System.out.println("8. August");
        System.out.println("9. September");
        System.out.println("10. October");
        System.out.println("11. November");
        System.out.println("12. December");
    }
 
    // Function to get the month input from the user
    static void getMonth()
    {
        try {
            System.out.print("Enter the month number: ");
            int monthNumber = scanner.nextInt();
 
            // Checking the month number and displaying the
            // corresponding month
            switch (monthNumber) {
            case 1:
                System.out.println("January");
                break;
            case 2:
                System.out.println("February");
                break;
            case 3:
                System.out.println("March");
                break;
            case 4:
                System.out.println("April");
                break;
            case 5:
                System.out.println("May");
                break;
            case 6:
                System.out.println("June");
                break;
            case 7:
                System.out.println("July");
                break;
            case 8:
                System.out.println("August");
                break;
            case 9:
                System.out.println("September");
                break;
            case 10:
                System.out.println("October");
                break;
            case 11:
                System.out.println("November");
                break;
            case 12:
                System.out.println("December");
                break;
            default:
                System.out.println(
                    "Invalid month number. Please enter a number between 1 and 12.");
            }
 
            // Asking the user if they want to continue or
            // exit the program
            System.out.print(
                "Enter Y to continue or any other character to exit: ");
            String choice = scanner.next();
 
            // If user wants to continue, recursively call
            // the getMonth function
            if (choice.equalsIgnoreCase("Y")) {
                getMonth();
            }
            else {
                // If user chooses to exit, display "Program
                // Terminated" and close the scanner
                System.out.println("Program Terminated");
                scanner.close();
            }
        }
        catch (NoSuchElementException e) {
            System.out.println(
                "No input available. Program terminated.");
        }
    }
 
    public static void main(String[] args)
    {
        // Display the initial menu
        displayMenu();
 
        // Start getting the month input from the user
        getMonth();
    }
}


Python




# Function to display the menu of months
def display_menu():
    print("Enter the number corresponding to a month:")
    print("1. January")
    print("2. February")
    print("3. March")
    print("4. April")
    print("5. May")
    print("6. June")
    print("7. July")
    print("8. August")
    print("9. September")
    print("10. October")
    print("11. November")
    print("12. December")
 
# Function to get the month input from the user
def get_month():
    while True:
        try:
            month_number = int(input("Enter the month number: "))
 
            # Checking the month number and displaying the corresponding month
            if 1 <= month_number <= 12:
                months = ["January", "February", "March", "April", "May", "June",
                          "July", "August", "September", "October", "November", "December"]
                print(months[month_number - 1])
            else:
                print("Invalid month number. Please enter a number between 1 and 12.")
 
            # Asking the user if they want to continue or exit the program
            choice = input("Enter Y to continue or any other character to exit: ")
             
            # If the user wants to exit, break out of the loop
            if choice.upper() != 'Y':
                print("Program Terminated")
                break
 
        except ValueError:
            print("Invalid input. Please enter a valid integer.")
 
# Display the initial menu
display_menu()
 
# Start getting the month input from the user
get_month()


C#




using System;
 
class Program
{
    static void Main(string[] args)
    {
        int monthNumber;
 
        // Display the menu
        Console.WriteLine("Enter the number corresponding to a month: ");
        Console.WriteLine("1. January");
        Console.WriteLine("2. February");
        Console.WriteLine("3. March");
        Console.WriteLine("4. April");
        Console.WriteLine("5. May");
        Console.WriteLine("6. June");
        Console.WriteLine("7. July");
        Console.WriteLine("8. August");
        Console.WriteLine("9. September");
        Console.WriteLine("10. October");
        Console.WriteLine("11. November");
        Console.WriteLine("12. December");
         
        while (true)
        {
            // Get user input
            Console.Write("Enter the month number: ");
            monthNumber = Convert.ToInt32(Console.ReadLine());
 
            // Check the input and output the corresponding month
            switch (monthNumber)
            {
                case 1:
                    Console.WriteLine("January");
                    break;
                case 2:
                    Console.WriteLine("February");
                    break;
                case 3:
                    Console.WriteLine("March");
                    break;
                case 4:
                    Console.WriteLine("April");
                    break;
                case 5:
                    Console.WriteLine("May");
                    break;
                case 6:
                    Console.WriteLine("June");
                    break;
                case 7:
                    Console.WriteLine("July");
                    break;
                case 8:
                    Console.WriteLine("August");
                    break;
                case 9:
                    Console.WriteLine("September");
                    break;
                case 10:
                    Console.WriteLine("October");
                    break;
                case 11:
                    Console.WriteLine("November");
                    break;
                case 12:
                    Console.WriteLine("December");
                    break;
                default:
                    Console.WriteLine("Invalid month number. Please enter a number between 1 and 12.");
                    break;
            }
 
            Console.WriteLine("Enter Y to continue");
            Console.WriteLine("Enter any other character to exit");
            char choice = Console.ReadKey().KeyChar;
            Console.WriteLine();
            if (char.ToUpper(choice) != 'Y')
                break;
        }
 
        Console.WriteLine("Program Terminated");
    }
}


Javascript




// Importing the 'readline' module for reading input from the console
const readline = require('readline');
 
// Creating an interface to read user input
const rl = readline.createInterface({
  input: process.stdin,
  output: process.stdout
});
 
// Function to display the menu of months
function displayMenu() {
  console.log("Enter the number corresponding to a month: ");
  console.log("1. January");
  console.log("2. February");
  console.log("3. March");
  console.log("4. April");
  console.log("5. May");
  console.log("6. June");
  console.log("7. July");
  console.log("8. August");
  console.log("9. September");
  console.log("10. October");
  console.log("11. November");
  console.log("12. December");
}
 
// Function to get the month input from the user
function getMonth() {
  rl.question("Enter the month number: ", function (monthNumber) {
    // Checking the month number and displaying the corresponding month
    switch (parseInt(monthNumber)) {
      case 1:
        console.log("January");
        break;
      case 2:
        console.log("February");
        break;
      case 3:
        console.log("March");
        break;
      case 4:
        console.log("April");
        break;
      case 5:
        console.log("May");
        break;
      case 6:
        console.log("June");
        break;
      case 7:
        console.log("July");
        break;
      case 8:
        console.log("August");
        break;
      case 9:
        console.log("September");
        break;
      case 10:
        console.log("October");
        break;
      case 11:
        console.log("November");
        break;
      case 12:
        console.log("December");
        break;
      default:
        console.log("Invalid month number. Please enter a number between 1 and 12.");
    }
 
    // Asking the user if they want to continue or exit the program
    rl.question("Enter Y to continue or any other character to exit: ", function (choice) {
      // If user wants to continue, recursively call the getMonth function
      if (choice.toUpperCase() === 'Y') {
        getMonth();
      } else {
        // If user chooses to exit, display "Program Terminated" and close the readline interface
        console.log("Program Terminated");
        rl.close();
      }
    });
  });
}
 
// Display the initial menu
displayMenu();
 
// Start getting the month input from the user
getMonth();


Output:

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



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads