Page 501 - Cs_withBlue_J_C11_Flipbook
P. 501

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:
                 Number: 28
                 28 is Perfect.
                                                            Variable Description
                   NAME         TYPE                                       DESCRIPTION

                  n          int          Number to be checked
                  num        int          Value of n is assigned to num
                  i          int          Finds the sum of the factors of the variable




                                                                                                                       499
                                                                                                   Internal Assessment  499
   496   497   498   499   500   501   502   503   504   505   506