Page 193 - CA_Blue( J )_Class10
P. 193

Ans.  import java.util.*;
                       class question6
                       {
                           public static void main()
                           {
                               Scanner sc=new Scanner(System.in);
                               String str,tstr="";
                               int l,i;
                               char ch1,ch2;
                               System.out.print("Enter the string: ");
                               str=sc.nextLine().toLowerCase();
                               str=" "+str;
                               l=str.length();
                               for(i = 0; i < l; i++)
                               {
                                   ch1=str.charAt(i);
                                   if(ch1==' ')
                                   {
                                           tstr = tstr+' '+Character.toUpperCase(str.charAt(i+1));
                                       i++;
                                   }
                                   else
                                       tstr += str.charAt(i);
                               }
                               System.out.println(tstr.trim());
                           }
                       }
                    22.   Write a menu-driven program to display the pattern as per the user’s choice:
                             Pattern 1           Pattern 2
                             ABCDE               B
                             ABCD                LL
                             ABC                 UUU
                             AB                  EEEE
                           A
                        For an incorrect option, an appropriate error message should be displayed.                  [2018]
                   Ans.  import java.util.*;
                       class question_8
                       {
                           public static void main() {
                               Scanner sc= new Scanner(System.in);
                               String str;
                               int ch,i,j;
                               char ch1;
                               System.out.println("Menu");
                               System.out.println("1. Pattern 1");
                               System.out.println("2. Pattern 2");
                               System.out.print("Enter your choice: ");
                               ch = sc.nextInt();
                               switch(ch)
                               {
                                   case 1:
                                   for(i=5;i>=1;i--) {
                                       for(j=1;j<=i;j++) {
                                           System.out.print((char)(64+j));
                                       }
                                       System.out.println();
                                   }
                                   break;


                                                                                                                       191
                                                                                              Iterative constructs in Java  191
   188   189   190   191   192   193   194   195   196   197   198