Computer Applications

Write a program to display the following patterns as per the user's choice.

Pattern 1           
I
 C
   S
     E

Pattern 2
      I
    C
  S
E

Java

Java Iterative Stmts

1 Like

Answer

import java.util.Scanner;

public class KboatMenuPattern
{
    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 ch = in.nextInt();
        String str = "ICSE";
        int len = str.length();
        switch (ch) {
            case 1:
                for (int i = 0; i < len; i++) {
                    for (int j = 0; j < i; j++) {
                        System.out.print(" ");
                    }
                    System.out.println(str.charAt(i));
                }
                break;

            case 2:
                for (int i = len - 1; i >= 0; i--) {
                    for (int j = 0; j < i; j++) {
                        System.out.print(" ");
                    }
                    System.out.println(str.charAt(len - 1 - i));
                }
                break;

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

        }
    }
}

Output

Answered By

1 Like


Related Questions