Page 575 - ComputerScience_Class_11
P. 575

6           {

                   7               num = n;
                   8           }

                   9           int sumOfFactors(int i)//finding sum of factor
                   10          {
                   11              if(i == 1)

                   12                  return 1 + sumOfFactors(i + 1);
                   13              else if(i < num && num % i == 0)
                   14                  return i + sumOfFactors(i + 1);

                   15              else if(i < num && num % i != 0)
                   16                  return 0 + sumOfFactors(i + 1);
                   17              else

                   18                  return 0;
                   19          }
                   20          void check()

                   21          {
                   22              if(num == sumOfFactors(1))
                   23                  System.out.println(num + " is Perfect.");

                   24              else
                   25                  System.out.println(num + " is not Perfect.");
                   26          }

                   27          public static void main(String args[])
                   28          {//start of main
                   29              Scanner sc=new Scanner(System.in);

                   30              System.out.print("Number: ");
                   31              int n = sc.nextInt();

                   32              Perfect obj = new Perfect(n);
                   33              obj.check();
                   34          }//end of main

                   35      }//end of class
                 The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java

                   Options

                  Number: 28
                  28 is Perfect.







                                                                                                Internal Assessment  573
   570   571   572   573   574   575   576   577   578   579   580