Wednesday, 28 October 2020

Write a menu driven program to print the following pattern according to user’s choice.

 Choice 1: 

A A A A A 1
B B B B 1 2
C C C 1 2 3
D D 1 2 3 4
E 1 2 3 4 5

Choice 2:

543212345
5432  2345
543      345
54          45
5              5


import java.util.*;
public class Assign6  

    public static void main(String[] args) 
    { 
        Scanner sc = new Scanner(System.in);
        System.out.println("Enter your choice");
        String choice = sc.next(); 
        int i,j,n;
 
        
        switch(choice) 
        { 
            case "1": 
                System.out.println("Enter the no of lines");
                 n=sc.nextInt();
                 for(i=1;i<=n;i++)
                 {
                     for(j=i;j<=n;j++)
                     {
                         System.out.print((char)(i+64));
                        }
         
                        for (int y = 1; y <= i; y++)
                        {
                            System.out.print(y);
                        }
                      
                        System.out.println("");
                    }
                        break; 
                        case "2": 
                        System.out.println("Enter the number of rows to print the pattern ");
                        int rows = sc.nextInt();       
               
                        for (int x = rows; x >= 1; x--)
                            {
                                for (int y = rows; y >= 1+rows-x; y--)
                                {
                                    System.out.print(y);
                                }
            
                                for (int y= x*2 ; y < rows*2-1; y++)
                                {
                                    System.out.print(" ");
                                }
            
                                for (int l = 1+rows-x; l <=rows; l++)
                                {
                                    if(l!=1)
                                    System.out.print(l);
                                }
                                System.out.println();
                            } 
                            break;
                            default: 
                            System.out.println("no match"); 
        } 
    } 

No comments:

Post a Comment