Computer Applications
Write a menu-driven program to display the following patterns as per the user’s choice:
(a)
1234567
12345
123
1
(b)
11111
22222
33333
44444
55555
Java
Java Nested for Loops
13 Likes
Answer
import java.util.Scanner;
public class KboatPatterns
{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.println("Type 1 for Pattern 1");
System.out.println("Type 2 for Pattern 2");
System.out.print("Enter your choice: ");
int ch = in.nextInt();
switch (ch) {
case 1:
for (int i = 7; i >= 1; i -= 2) {
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
break;
case 2:
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= 5; j++) {
System.out.print(i);
}
System.out.println();
}
break;
default:
System.out.println("Incorrect Choice");
}
}
}
Output
Answered By
4 Likes