Computer Science, asked by nivedita4122003, 1 year ago

Write a program to print following patterns with the help of users choice statement(switch-case). Print appropriate menu with options.
(i) using for loop (ii) using while loop
1 2 3 4 5 A
1 2 3 4 A B
1 2 3 A B C
1 2 A B C D
1 A B C D E


QGP: Which language do you want it in? I can help with JAVA
nivedita4122003: I want it in Java.
nivedita4122003: Please help ASAP
QGP: Oh okay.. I will do it soon.
nivedita4122003: thanks...

Answers

Answered by QGP
27
Here's the Java code for the required program. The description is given as comments. Paste it into an IDE, so that code and comments can be seen properly.



import java.util.Scanner;  //Importing Scanner for input
public class Brainly        //Creating a Class
{
    public static void main(String[] args)      //Creating main() function
    {
        Scanner sc = new Scanner(System.in);        //Declaring Scanner Object

        System.out.println("1. Print Pattern using for loop");      //Printing Menu Options
        System.out.println("2. Print Pattern using while loop");
        System.out.println("3. Exit");

        System.out.print("\nEnter your choice: ");          //Asking User for Choice Input
        int choice = sc.nextInt();                          //Storing User Input in choice

        System.out.println();
       
        int i,j,k,ch;               //Variables to be used for pattern printing
       
        switch(choice)
        {
            case 1:         //for loop case

           
            for(i=5;i>0;i--)        //Outermost loop of i prints 5 rows
            {
                for(j=1;j<=i;j++)       //The loop of j prints the numbers in each row
                {
                    System.out.print(j+" ");
                }
               
                ch=65;      //65 is the ASCII Code for "A"
               
                for(k=5;k>=i;k--)       //The loop of k prints the characters in each row
                {
                    System.out.print((char)ch+" ");         //ASCII Value is casted into char type and printed
                    ch++;
                }
                System.out.println();
            }
           
            break;
           
           
            case 2:             //while loop case
            i=5;
            while(i>0)
            {
                j=1;
                while(j<=i)
                {
                    System.out.print(j+" ");
                    j++;
                }
               
               
                ch=65;      //65 is the ASCII Code for "A"
               
                k=5;
                while(k>=i)
                {
                    System.out.print((char)ch+" ");
                    ch++;
                    k--;
                }
               
                System.out.println();
                i--;
            }
           
            break;
           
            case 3:         //Exit case
            break;

            default:        //Default Case for Invalid Choice
            System.out.println("Invalid Choice");
        }
    }
}


Similar questions