Page 574 - ComputerScience_Class_11
P. 574

22                   else

                23                       continue;
                24               }

                25               System.out.println("the sum of odd numbers in the even positions is "+s);
                26           }      //end of main
                27       }     //end of class

              The output of the preceding program is as follows:

                     BlueJ: Terminal Window - Java
                 Options

                enter the size of the array
                5
                enter the array elements
                22
                77
                44
                55
                11
                the sum of odd numbers in the even positions is 11



                Program 7      A perfect number is a positive integer that is equal to the sum of its proper divisors. The
                               smallest perfect number is 6, which is the sum of 1, 2 and 3. Other perfect numbers are 28,
                               496 and 8,128.
                               Class name                      :   Perfect

                               Data members/Instance variables
                               num                             :   to store the number
                               Methods/Member functions
                               Perfect(int n)                  :    parameterised constructor to  initialise  the data
                                                                 member num = n
                               int sumOfFactors(int i)         :    returns the sum of the factors of the number (num),
                                                                 excluding itself, using the recursive technique
                               void check()                    :   checks whether the given number is perfect by invoking
                                                                 the function sumOfFactors(int) and displays the result
                                                                 with an appropriate message
                               Specify the class Perfect, giving details of the constructor, int sumOfFactors(int) and void
                               check().
                               Define the main() function to create an object and call the functions accordingly to enable
                               the task.
                 1       import java.util.*;

                 2       class Perfect
                 3       {//start of class
                 4           int num;

                 5           Perfect(int n)



                  572  Touchpad Computer Science (Ver. 3.0)-XI
   569   570   571   572   573   574   575   576   577   578   579