Page 194 - CA_Blue( J )_Class10
P. 194

case 2:
                                str = "BLUE";
                                for(i = 0; i < str.length(); i++) {
                                    ch1 = str.charAt(i);
                                    for(j = 0; j <= i; j++)
                                        System.out.print(ch1);
                                    System.out.println();
                                }
                                break;
                                default:
                                System.out.println("Wrong Choice");
                            }
                        }
                    }
                 23.  Write a program to accept a number and check and display whether it is a spy number or not. A number is spy if the sum of its
                    digits equals the product of its digits.
                     Example: Consider the number 1124.
                     Sum of the digits = 1 + 1 + 2 + 4 = 8.
                     Product of the digits = 1 × 1 × 2 × 4 = 8                                                    [2017]
                Ans. import java.util.*;
                    class Spy{
                        public static void main() {
                            Scanner sc= new Scanner(System.in);
                            int n,s=0,p=1,i,r;
                            System.out.print("Enter the number: ");
                            n = sc.nextInt();
                            i=n;
                            while(i>0) {
                                r = i % 10;
                                s=s+r;
                              p=p*r;
                                i=i/10;
                            }
                            if(s == p)
                                System.out.println(n+ " is Spy number.");
                            else
                                System.out.println(n+" is not spy number.");
                        }
                    }
                 24.  Using the switch statement, write a menu-driven program for the following:
                    (i)   To find and display the sum of the series given below:
                                 3
                                    4
                          1
                              2
                                        5
                                             20
                       S = x  – x  + x  – x  + x  … – x , where x = 2.
                    (ii) To display the following series:
                       1 11 111 1111 11111
                     For an incorrect option, an appropriate error message should be displayed.                   [2017]
                Ans. import java.util.*;
                    class series{
                        public static void main() {
                             Scanner sc= new Scanner(System.in);
                            int ch,i,p=0;
                              double s=0.0,x=2;
                               System.out.println("1. Series 1");
                             System.out.println("2. Series 2");
                             System.out.print("Enter your choice: ");
                            ch = sc.nextInt();
                            switch(ch) {
                             case 1:
                                for(i = 1; i <= 20; i++)


                192192  Touchpad Computer Applications-X
   189   190   191   192   193   194   195   196   197   198   199