Home / Programs / Using the switch-case statement, write a menu driven program to do the following:
Programming Example

Using the switch-case statement, write a menu driven program to do the following:

👁 130 Views
💻 Practical Program
📘 Step by Step Learning
Study this program carefully to understand the logic, output, and explanation in a structured way.

Information & Algorithm

Using the switch-case statement, write a menu driven program to do the following:

(a) To generate and print Letters from A to Z and their Unicode

Letters Unicode
A 65
B 66
. .
. .
. .
Z 90

(b) Display the following pattern using iteration (looping) statement:

1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

Program Code

import java.util.Scanner;

public class RAnsariSwitchCase
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 1 for letters and Unicode");
        System.out.println("Enter 2 to display the pattern");
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        switch(ch) {
            case 1:
            System.out.println("Letters\tUnicode");
            for (int i = 65; i <= 90; i++)
                System.out.println((char)i + "\t" + i);
            break;

            case 2:
            for (int i = 1; i <= 5; i++) {
                for (int j = 1; j <= i; j++)
                    System.out.print(j + " ");
                System.out.println();
            }
            break;

            default:
            System.out.println("Wrong choice");
            break;
        }
    }
}

Output

Enter 1 for letters and Unicode
Enter 2 to display the pattern
Enter your choice: 1
Letters Unicode
A       65
B       66
C       67
D       68
E       69
F       70
G       71
H       72
I       73
J       74
K       75
L       76
M       77
N       78
O       79
P       80
Q       81
R       82
S       83
T       84
U       85
V       86
W       87
X       88
Y       89
Z       90
Press any key to continue . . .

How to learn from this program

First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.