Q&A ICSE2018-B08 #7196
Question:
Write a menu driven program to display the pattern as per user’s choice.
Pattern 1 Pattern 2
ABCDE B
ABCD LL
ABC UUU
AB EEEE
A
For an incorrect option, an appropriate error message should be displayed.
Answer:
public class Pattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter pattern number: ");
int choice = scanner.nextInt();
if (choice == 1) {
System.out.print("Enter number of lines: ");
int n = scanner.nextInt();
for (int i = n; i >= 1; i--) {
for (int j = 0; j < i; j++) {
System.out.print((char) ('A' + j));
}
System.out.println();
}
} else if (choice == 2) {
System.out.print("Enter string: ");
String s = scanner.next();
for (int i = 0; i < s.length(); i++) {
char ch = s.charAt(i);
for (int j = 1; j <= (i + 1); j++) {
System.out.print(ch);
}
System.out.println();
}
} else {
System.out.println("Invalid choice");
}
}
}
