Page 197 - CA_Blue( J )_Class10
P. 197

28.  The International Standard Book Number (ISBN) is a unique numeric book identifier which is printed on every book. The ISBN
                       is based upon a 10-digit code. The ISBN is legal if: 1 × digit1 + 2 × digit2 + 3 × digit3 + 4 × digit4 + 5 × digit5 + 6 × digit6 + 7 × digit7
                       + 8 × digit8 + 9 × digit9 + 10 × digit10 is divisible by 11.
                        Example: For an ISBN 1401601499, sum = 1 × 1 + 2 × 4 + 3 × 0 + 4 × 1 + 5 × 6 + 6 × 0 + 7 × 1 + 8 × 4 + 9 × 9 + 10 × 9 = 253 which is
                       divisible by 11.                                                                             [2013]
                        Write a program to:
                       (i)  Input the ISBN code as a 10-digit integer.
                       (ii)   If the ISBN is not a 10-digit integer, output the message "Illegal ISBN" and terminate the program.
                       (iii)  If the number is divisible by 11, output the message "Legal ISBN". If the sum is not divisible by 11, output the message
                          "Illegal ISBN".
                   Ans. import java.util.*;
                       class ISBN{
                           public static void main(){
                                Scanner sc= new Scanner(System.in);
                                long n,t,sum=0L;
                                int r,pos=10;
                                System.out.print("Enter a number :  ");
                                n = sc.nextLong();
                                if(Long.toString(n).length() != 10)
                                {   System.out.println("Illegal ISBN");   }
                                else
                                {   t=n;
                                   while(t > 0)
                                    {   sum = sum + (t % 10 * pos);
                                      t = t / 10;
                                      pos--;
                                   }
                                   if(sum % 11 == 0)
                                        System.out.println("Legal ISBN");
                                   else
                                        System.out.println("Illegal ISBN");
                               }
                           }
                       }
                    29.  Using the switch statement, write a menu-driven program:
                       (i)   To check and display whether a number input by the user is a composite number or not. (A number is said to be composite
                         if it has one or more than one factor excluding 1 and the number itself.). Example: 4, 6, 8, 9, …
                       (ii)  To find the smallest digit of an integer that is input:
                          Sample Input: 6524
                          Output: Smallest digit is 2.
                        For an incorrect choice, an appropriate error message should be displayed.                  [2013]
                   Ans. import java.util.*;
                       class switchcase{
                           public static void main(){
                               Scanner sc= new Scanner(System.in);
                               int ch,n,i,c=0,s=10,r;
                               System.out.println("1. Composite Number");
                               System.out.println("2. Smallest Digit");
                               System.out.print("Enter your choice: ");
                               ch= sc.nextInt();
                               switch(ch){
                                   case 1:
                                   System.out.print("Enter a number: ");
                                   n = sc.nextInt();
                                   for(i = 2; i < n; i++){
                                       if(n % i == 0)
                                           c++;
                                   }


                                                                                                                       195
                                                                                              Iterative constructs in Java  195
   192   193   194   195   196   197   198   199   200   201   202