ICSE Class 10 Computer Applications
Question 24 of 69
Iterative Constructs in Java — Question 24
Back to all questions 24
Question Question 20
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
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

