icse-promo

Question

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
11 12 13 14 15

(ii) To display the following pattern

I
I C
I C S
I C S 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 program5
{
    public static void main(String[] args)
    {
        int choice=0,i=0,j=0,n=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("Press 1 for Floyd's triangle");
        System.out.println("Press 2 to print  ICSE Pattern");
        System.out.print("Enter your choice: ");
        choice = sc.nextInt();
        switch (choice) 
        {
            case 1:
                System.out.print("Enter number of lines: ");
                n = sc.nextInt();
                int number = 1;
                for (i = 1; i <= n; i++)
                {
	                for (j = 1; j <= i; j++)
	                {
		                System.out.print(number + " ");
		                number++;
	                }
	                System.out.println();
                }
                break;
            
            case 2:
                System.out.print("Enter a  word: ");
                String word = sc.next();
                for (i = 0; i < word.length(); i++) 
                {
	                for (j = 0; j <= i; j++)
	                {
		                System.out.print(word.charAt(j) + " ");
	                }
	                System.out.println();
                }
                break;

            default:
                System.out.println("Invalid choice");
                break;
        }
    }
}

				
			

Coding Store

Leave a Reply

Your email address will not be published. Required fields are marked *