Question 8.
Write a menu driven program to display the pattern as per user’s choice.
ICSE Computer Applications Question Paper 2018 Solved for Class X
For an incorrect option, an appropriate error message should be displayed.
import java.util.Scanner;
public class ICSEJava2018 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int choice = 0;
char[] array1 = { 'A', 'B', 'C', 'D', 'E' };
char[] array2 = { 'B', 'L', 'U', 'E' };
System.out.println("Press 1 to generate the first pattern:");
System.out.println("Press 2 to generate the second pattern::");
System.out.println("Enter your choice:");
choice = in.nextInt();
switch (choice) {
case 1:
for (int i = 0; i < 5; i++) {
for (int j = 5 - i; j >= 1; j--) {
System.out.print(array1[5 - j]);
}
System.out.println();
}
break;
case 2:
int counter = 0;
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(array2[counter]);
}
counter++;
System.out.println();
}
break;
default:
System.out.println("WRONG CHOICE!:");
break;
}
in.close();
}
}
No comments:
Post a Comment