11. Using the switch statement, write a menu driven program for the following:
[ICSE 2016
(a) To print the Floyd's triangle:
(b) To display the following pattern:
1
I
2 3
I C
4 5 6
ICS
7 8 9 10
I CSE
11 12 13 14 15
For an incorrect option, an appropriate error message should be displayed.
Answers
Answered by
3
Question:-
- WAP a menu-driven program to perform the following:-
(a) To print Floyd's triangle
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
(b) To print the following pattern
I
IC
ICS
ICSE
Code:-
package Coder;
import java.util.*;
public class StrPattern {
public static void main(String[] args) {
Scanner sc=new Scanner (System.in);
int i,j,ch,c=0;
System.out.println("Enter '1' for Floyd's triangle, '2' for pattern");
ch=sc.nextInt();
switch (ch)
{
case 1:
System.out.println("Enter number of rows");
int n=sc.nextInt();
for(i=1;i<=n;i++)
{
for(j=1;j<=i;j++)
{
System.out.print(c+" ");
c++;
}
System.out.println();
}
break;
case 2:
String str="ICSE";
for(int k=1;k<=str.length();k++)
{
System.out.println(str.substring(0,k));
}
break;
default:System.out.println("Invalid choice");
}
}
}
Variable Description:-
- i:- loop variable
- j:- loop variable
- k:- loop variable
- c:- count variable used to print the numbers from 1 to n
- str:- used to store the string
•Output attached
Attachments:
Similar questions