Page 500 - Cs_withBlue_J_C11_Flipbook
P. 500

The output of the preceding program is as follows:
              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
                                                         Variable Description
                 NAME         TYPE                                       DESCRIPTION
               n           int          Size of array

               i           int          Loop variable
               s           int          Sum of the digit at that position is odd



                Program 6      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)
                 6           {

                 7               num = n;

                 8           }



                498498  Touchpad Computer Science-XI
   495   496   497   498   499   500   501   502   503   504   505