Page 571 - ComputerScience_Class_11
P. 571

Program 5     Define a class Smith to check whether a number is a Smith number or not.

                                 Eg: A Smith number is a composite number, the sum of whose digits is the sum of the digits
                                 of its prime factors obtained as a result of prime factorisation (excluding 1). The first few
                                 such numbers are 4, 22, 27, 58, 85, 94, 121, …
                                 Example:
                                 N = 666
                                 Prime factors are 2, 3, 3, 37
                                 Sum of the digits = 6 + 6 + 6 = 18
                                 Sum of the digits of the factors 2 + 3 + 3 + 3 + 7 = 18
                                 Hence, 666 is a Smith Number.
                                 The data members and the member methods are defined as follows:

                                 int n                           :   To store the number
                                 Member Methods
                                 boolean composite(int x)        :  To check whether a number is composite or not
                                 int sum_of_digits(int x)        :  To find the sum of the digits of a number
                                 int sum_prime(int x)            :  To find the sum of the prime factors of the number
                                 Also, write the main method to call the function.


                   1       import java.util.*;
                   2       class Smith
                   3       {

                   4           int n;
                   5           //Member methods

                   6           boolean composite(int n)
                   7           {

                   8               int flag = 0,i;
                   9               for(i = 1; i <= n; i++)

                   10                  if(n % i == 0)
                   11                      flag++;

                   12              if(flag > 2)
                   13                  return true;

                   14              return false;
                   15          }

                   16          int sum_of_digits(int n)
                   17          {

                   18              int s = 0,i=n;
                   19              while(i!=0)

                   20              {



                                                                                                Internal Assessment  569
   566   567   568   569   570   571   572   573   574   575   576