Computer Science, asked by maylina, 4 months ago

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 BrainlyProgrammer
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:-

  1. i:- loop variable
  2. j:- loop variable
  3. k:- loop variable
  4. c:- count variable used to print the numbers from 1 to n
  5. str:- used to store the string

•Output attached

Attachments:
Similar questions