Page 227 - ComputerScience_Class_11
P. 227

Program 12     Write a Java program to print the following using switch case:
                                   1.   1                  2.  A
                                       1 2                    A B
                                       1 2 3                  A B C
                                       1 2 3 4                A B C D

                   1      import java.util.*;
                   2      class nestedloop1    {

                   3          public static void main(String args[])    {
                   4              Scanner sc= new Scanner(System.in);

                   5              int ch, n, i, j;
                   6              double s=0, a;
                   7              System.out.println("1. Pattern 1");

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

                   9              System.out.println("Enter your choice ");
                  10              ch=sc.nextInt();
                  11              switch(ch)    {

                  12                  case 1:
                  13                      for(i=1; i<=4; i++)    {

                  14                          for(j=1; j<=i; j++)    {
                  15                              System.out.print(j+ " ");

                  16                          }
                  17                          System.out.println();

                  18                      }
                  19                      break;

                  20                  case 2:
                  21                      for(i=1; i<=5; i++)    {

                  22                          for(j=1; j<=i; j++)    {
                  23                              System.out.print((char)(64+j)+" ");

                  24                          }
                  25                          System.out.println();

                  26                      }
                  27                      break;
                  28                  default: System.out.println("Wrong Choice");

                  29              }

                  30          }
                  31      }




                                                                                              Statements and Scope  225
   222   223   224   225   226   227   228   229   230   231   232