Page 444 - Computer science 868 Class 12
P. 444

for(int i=0;i<n;i++)
                       { System.out.print(arr[i]+" " );}
                       System.out.println();
                     }
                     int bin_search(int l,int u, int v )
                     {
                       int m=(l+u)/2;
                       if(arr[m]==v)  // if found
                        return m;
                       else if(l>u)  // if lower limit > upper limit
                        return -1;
                       else if (arr[m]>v)  // number is less than the number in middle
                        return bin_search(l,m-1,v);
                       else
                        return bin_search(m+1,u,v); // number is greater than the number in middle
                     }
                     public static void main(int s,int v)
                     {
                       BinSearch obj = new BinSearch(s);
                       obj.fillarray();
                       obj.sort();
                       int p=obj.bin_search(0,s,v);
                       if(p==-1)
                       System.out.println(v+"not found in array ");
                       else
                       System.out.println(v+" found in location: " +(p+1));
                     }
                    }
                  6.  Design a class ArmNum to check if a given number is an Armstrong number or not. [A number is said to be Armstrong if sum
                    of its digits raised to the power of length of the number is equal to the number].         [ISC 2019]
                     Example:
                          3
                             3
                     371 = 3  + 7  + 1 3
                           4
                              4
                                  4
                     1634 = 1  + 6  + 3  + 4 4
                            5
                               5
                                      5
                     54748 = 5  + 4  + 7  + 4  + 8 5
                                   5
                     Thus, 371, 1634 and 54748 are all examples of Armstrong numbers. Some of the members of the class are given below:
                     Class name                       :  ArmNum
                     Data Members/Instance variables
                     n                                :   to store the number
                     l                                :   to store the length of the number
                     Methods/Member functions
                     ArmNum (int nn)                  :   parameterised constructor to initialise the data member n = nn
                     int sumPow(int i)                :     returns the sum of each digit raised to the power of the length of the number using
                                                         recursive technique eg., 34 will return 3  + 4  (as the length of the number is 2)
                                                                                       2
                                                                                          2
                     void isArmstrong()               :    checks whether the given number is an Armstrong number by invoking the
                                                         function sumPow() and displays the result with an appropriate message
                     Specify the class ArmNum giving details of the constructor( ), int sumPow(int) and void isArmstrong( ). Define a main() function
                    to create an object and call the functions accordingly to enable the task.
                Ans.  import java.util.Scanner;
                    import java.io.*;
                    class ArmNum
                    {
                         private int n;
                     private int l;


                442442  Touchpad Computer Science-XII
   439   440   441   442   443   444   445   446   447   448   449