Page 446 - Computer science 868 Class 12
P. 446

Ans.  Dry run of the function
                     n = 43629, x = 3, y = 4
                      Step   num       x      y     num<10     z     z%2          Return          return value
                       1.    43629     3      4      False     9      1    36+ Mystery(4362, 3, 4)  36 + 40 =76
                       2.    4362      3      4      False     2      0    6+ Mystery(436, 3, 4)  6 + 34 = 40
                       3.     436      3      4      False     6      0    18+ Mystery(43, 3, 4)  18 + 16 = 34
                       4.     43       3      4      False     3      1    12+ Mystery(4, 3, 4)  12 + 4 = 16
                       5.      4       3      4                                                 4
                  8.  Design a class Perfect to check if a given number is a perfect number or not. [A number is said to be perfect if sum of the
                    factors of the number excluding itself is equal to the original number]. Example: 6 = 1 + 2 + 3 (where 1, 2 and 3 are factors of
                    6, excluding itself)  Some of the members of the class are given below:                    [ISC 2018]
                     Class name                       :  Perfect
                     Data Members/Instance variables
                     num                              :   to store the number
                     Methods/Member functions
                     Perfect (int nn)                 :  parameterised constructor to initialise the data member num=nn
                     int sum_of_factors(int i)        :     returns the sum of the factors of the number(num), excluding itself, using a
                                                         recursive technique
                     void check()                     :    checks whether the given number is perfect by invoking the function sum_of_
                                                         factors() and displays the result with an appropriate message
                     Specify the class Perfect giving details of the constructor(), int sum_of_factors(int) and void check(). Define a main() function
                    to create an object and call the functions accordingly to enable the task.

                Ans.  import java.util.*;
                    class Perfect
                    {
                     int num;
                     Perfect(int nn)
                     {
                     num=nn;
                     }
                     int sum_of_factors(int i)
                     {
                     if (i == num)
                     return 0;
                     else if (num%i==0)
                     return i + sum_of_factors(i+1);
                     else
                     return sum_of_factors(i+1);
                     }
                     void check()
                     {
                     int s=sum_of_factors(1);
                     if (s==num)
                     System.out.println(num+"is a perfect number");
                     else
                     System.out.println(num+"is not a perfect number");
                     }
                     public static void main(String[] args)
                     {
                     Scanner sc=new Scanner(System.in);
                     System.out.println("Enter a number");
                     int no=sc.nextInt();
                     Perfect ob=new Perfect(no); // OR Perfect ob=new Perfect(6);
                     ob.check(); // ob.check();
                     }

                444444  Touchpad Computer Science-XII
   441   442   443   444   445   446   447   448   449   450   451