CodeHub Menu
Home Exercise Practice About Download Portfolio Play Games NEW Privacy Policy Terms Contact
Login / Register

Menu based Pattern using JAVA

JAVA November 21, 2025 4 views 0 likes
Using the switch statement, write a menu driven program for the following:
(1) To print the Floyd’s triangle [Given below]
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

(2) To display the following pattern:
I
I C
I C S
I C S E

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

Answer

Pattern - Java Code

Pattern - Java Code


import java.util.Scanner;

public class Pattern 
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.println("Type 1 for Floyd's triangle");
        System.out.println("Type 2 for an ICSE pattern");
        
        System.out.print("Enter your choice: ");
        int ch = in.nextInt();
        
        switch (ch) {
            case 1:
                int a = 1;
                for (int i = 1; i <= 5; i++) {
                    for (int j = 1; j <= i; j++) {
                        System.out.print(a++ + "\t");
                    }
                    System.out.println();
                }
                break;
            
            case 2:
                String s = "ICSE";
                for (int i = 0; i < s.length(); i++) {
                    for (int j = 0; j <= i; j++) {
                        System.out.print(s.charAt(j) + " ");
                    }
                    System.out.println();
                }
                break;
            
            default:
                System.out.println("Incorrect Choice");
        }
    }
}

OUTPUT:


Pattern Generator

Pattern Generator

Output:


4 views 0 likes
Total visits: 2,089 • Unique visitors: 1,285