Question 5.
Using the switch statement, write a menu driven
program for the following:
(i) To print the Floyd's triangle (given below] :
1
2 3
4 5 6
7 8 9 10
(ii) To display the following pattern
I
IC
ICS
ICSE
Answers
Answered by
22
Explanation:
import java.util.*;
class Choice
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter your choice\n1-Floyd's triangle\n2-pattern");
int n=sc.nextInt();
switch(n)
{
case 1:int a=1;
for(int i=1;i<=4;i++)
{
for(int j=1;j<=i;j++)
System.out.print(a++ +" ");
System.out.println();
}
break;
case 2:String s="ICSE";
for(int i=0;i<=4;i++)
{
System.out.println(s.substring(0,i));
}
break;
default:System.out.println("Invalid choice");
}
}
}
Attachments:
Similar questions