Question
Write a menu-driven program to display the pattern as per user’s choice:
Pattern 1
ABCDE
ABCD
ABC
AB
A
Pattern 2
B
L L
U U U
E E E
For an incorrect option, an appropriate error message should be displayed.
Share code with your friends
Share on whatsapp
Share on facebook
Share on twitter
Share on telegram
Code
import java.util.Scanner;
public class program8
{
public static void main(String[]args)
{
int choice=0,i=0,j=0,num=0;
char k=' ',ch=' ';
String wd="";
Scanner sc=new Scanner(System.in);
System.out.println("Press 1 or 2 to choose a pattern: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter number of lines: ");
num = sc.nextInt();
for(i=1;i<=num;i++)
{
k='A';
for(j=1;j<=num;j++)
{
if(j<=num+1-i)
{
System.out.print(k);
k++;
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
break;
case 2:
System.out.print("Enter a word: ");
wd=sc.next();
for(i=0;i< wd.length();i++)
{
ch=wd.charAt(i);
for(j=0;j< wd.length();j++)
{
if(j<=i)
{
System.out.print(ch);
}
else
{
System.out.print(" ");
}
}
System.out.println();
}
break;
default:
System.out.println("Invalid coice");
}
}
}
Coding Store
Sale
ISC QUESTION PAPERS WITH SOLUTION(PROGRAMMING ONLY)
Sale