Page 238 - Computer science 868 Class 12
P. 238

{
                               System.out.println("Invalid Input");
                               return 0;
                           }
                           int count = 0, p = 0, binNum = 0,d;
                           while (n > 0)
                           {
                               d = n % 2;
                               if (d == 1)
                                   count++;
                               binNum += (int)(d * Math.pow(10, p));
                               p++;
                               n /= 2;
                           }
                           System.out.println("Binary Equivalent: " + binNum);
                           System.out.println("No. of 1's: " + count);
                           if (count % 2 == 0)
                               return 1;
                           else
                               return 2;
                       }

                       public static void main(String args[])
                       {
                           Scanner sc = new Scanner(System.in);
                           int n,ch;
                           System.out.print("Enter a positive number: ");
                           n = sc.nextInt();
                           ch=check(n);
                           if(ch==1)
                               System.out.println(n+ " Evil Number ");
                           else
                               System.out.println(n+ " Not Evil Number ");
                       }
                   }
                2.  A Smith number is a composite number whose sum of the digits is equal to the sum of its prime factors. For
                   example,
                    4, 22, 27, 58, 85, 94, 121 ………. are Smith numbers.
                    Write a method in Java to enter a number and check whether it is a Smith number or not.
                    Sample Input: 666
                    Sum of the digits: 6 + 6 + 6 = 18
                    Prime factors are: 2, 3, 3, 37
                    Sum of the digits of the prime factors: 2 + 3 + 3 + (3 + 7) = 18
                    Thus, 666 is a Smith Number.

              Ans.  import java.util.*;
                   class SmithNumber
                   {
                       void check(int n)
                       {


                236236  Touchpad Computer Science-XII
   233   234   235   236   237   238   239   240   241   242   243