Page 222 - CA_Blue( J )_Class10
P. 222

(ii)  To display the following pattern:
                         I
                         IC
                         ICS
                         ICSE
                     For an incorrect option, an appropriate error message should be displayed.                   [2016]
                Ans.  import java.util.*;
                     class switch_case {
                          public static void main(String args[])
                         {    Scanner sc= new Scanner(System.in);
                             int ch,k=1,i,j;
                             String s = "ICSE";
                              System.out.println("1. Floyd's Triangle");
                              System.out.println("2. ICSE");
                              System.out.print("Enter your choice: ");
                             ch = sc.nextInt();
                             switch(ch)
                             {  case 1:
                                 for( i = 1; i <= 5; i++){
                                     for( j = 1; j <= i; j++){
                                          System.out.print(k + " ");
                                         k++;
                                    }
                                     System.out.println();
                                 }
                                 break;
                                 case 2:
                                  for(i = 0; i < s.length(); i++){
                                      for( j = 0; j <= i; j++){
                                          System.out.print(s.charAt(j));
                                    }
                                        System.out.println();
                                    }
                                    break;
                                    default:
                                     System.out.println("Wrong Choice");
                             }
                         }
                     }
                  5.  Write two separate programs to generate the following patterns using iteration (loop) statements:   [2015]
                     (a)  *
                         *#
                         *#*
                         *#*#
                         *#*#*
                     (b)  5 4 3 2 1
                         5 4 3 2
                         5 4 3
                         5 4
                         5
                Ans.  (a)  class Pattern1 {
                              public static void main(){
                                  int i,j;
                                  for(i = 1; i <= 5; i++){
                                      for(j = 1; j <= i; j++){
                                          if(j%2==0)
                                               System.out.print('#');


                220220  Touchpad Computer Applications-X
   217   218   219   220   221   222   223   224   225   226   227