Home / Programs / Using the switch statement, write a menu driven program for the following: (i) To print the Floyd’s triangle [Given below] 12   34   5   67   8   9  1011 12 13 14 15 (b) To display the following pattern:II CI C SI C S E For an incorrect option, an appropriate error message should be displayed.
🚀 Programming Example

Using the switch statement, write a menu driven program for the following:

(i) To print the Floyd’s triangle [Given below]

1
2   3
4   5   6
7   8   9  10
11 12 13 14 15

(b) 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.

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

💻 Program Code

import java.util.Scanner;

public class RAnsariPattern
{
    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");
        }
    }
}
                        

🖥 Program Output

Type 1 for Floyd's triangle
Type 2 for an ICSE pattern
Enter your choice: 1
1
2       3
4       5       6
7       8       9       10
11      12      13      14      15
Press any key to continue . . .


Type 1 for Floyd's triangle
Type 2 for an ICSE pattern
Enter your choice: 2
I
I C
I C S
I C S E
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.