Create a free account on Computer Sir Ki Class and unlock access to valuable learning resources.
Exam Paper Downloads: After registering and logging in, you can download exam question papers in PDF format to practice and prepare for your exams.
Student Support: Get assistance with your doubts, queries, and questions related to your studies. Our support is here to help you understand topics and resolve your learning difficulties.
Register today and make your exam preparation easier and more effective!
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");
}
}
}
C. Sir| Created: 12-Apr-2019 | Updated: 12-Apr-2019|ICSE2018