Computer Science, asked by tucctutuc, 5 months ago

13. Write a menu driven program to print the following patterns​

Attachments:

Answers

Answered by anindyaadhikari13
3

Required Answer:-

Question:

  • Write a menu driven program to display the given patterns.

Solution:

Here comes the program. It is written in Java.

import java.util.*;

public class Patterns {

 public static void main(String[] args) {

     int ch, n, i, j;

     Scanner sc=new Scanner(System.in);

     System.out.println("1. Pattern 1.");

     System.out.println("2. Pattern 2.");

     System.out.print("\nEnter your choice - ");

     ch=sc.nextInt();    

     switch(ch)  {

        case 1:

          System.out.print("Enter the number of rows - ");

          n=sc.nextInt();

          for(i=1;i<=n;i++) {

             for(j=1;j<=n-i;j++)

               System.out.print(" ");

             for(j=1;j<=i;j++)

               System.out.print("* ");

             System.out.println();

          }

        break;        

        case 2:

          System.out.print("Enter the number of rows - ");

          n=sc.nextInt();

          for(i=1;i<=n;i++)  {

             for(j=i;j>=1;j--)

               System.out.print(j+" ");

             System.out.println();

          }

        break;

        default: System.out.println("Invalid Choice.");

     }

     sc.close();

 }

}

Explanation:

  • Using switch case, the problem is solved. The user will enter his/her choice. If the choice is valid, then it will ask the user for the number of rows. Then, it will display the pattern. If the choice is invalid, an error message is displayed.
Attachments:
Similar questions