Home / Programs / Write a menu driven program to display the pattern as per user’s choice.
🚀 Programming Example

Write a menu driven program to display the pattern as per user’s choice.

👁 115 Views
💻 Practical Program
📘 Step Learning
Learn this program step-by-step with algorithm, source code, output and detailed explanation.

📌 Information & Algorithm

Write a menu driven program to display the pattern as per user’s choice.

Pattern 1

ABCDE
ABCD
ABC
AB
A

Pattern 2

B
LL
UUU
EEEE

For an incorrect option, an appropriate error message should be displayed.

💻 Program Code

import java.util.Scanner;

public class RansariMenuPattern
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Enter 1 for pattern 1");
        System.out.println("Enter 2 for Pattern 2");
        System.out.print("Enter your choice: ");
        int choice = in.nextInt();
        switch (choice) {
            case 1:
            for (int i = 69; i >= 65; i--) {
                for (int j = 65; j <= i; j++) {
                    System.out.print((char)j);
                }
                System.out.println();
            }
            break;

            case 2:
            String word = "BLUE";
            int len = word.length();
            for(int i = 0; i < len; i++) {
                for(int j = 0; j <= i; j++) {
                    System.out.print(word.charAt(i));
                }
                System.out.println();
            }
            break;

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

        }
    }
}
                        

🖥 Program Output

Enter 1 for pattern 1
Enter 2 for Pattern 2
Enter your choice: 1
ABCDE
ABCD
ABC
AB
A
Press any key to continue . . .



Enter 1 for pattern 1
Enter 2 for Pattern 2
Enter your choice: 2
B
LL
UUU
EEEE
Press any key to continue . . .

                            
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.