Page 429 - Cs_withBlue_J_C11_Flipbook
P. 429

return 1;
                           else if (n == 2)
                               return 1;
                           else
                               return 2*func(n-2) + func(n-3);
                       }

                 D.  Unsolved Programs:
                    1.  Design a class Armstrong to check if a given number is a Armstrong number or not. An Armstrong number is a positive m-digit
                       number that is equal to the sum of the m  powers of their digits. For example,
                                                      th
                                  3
                               3
                           3
                        153: 1  + 5  + 3  = 1 + 125 + 27 = 153 (Digit count of 153 is 3. So, cube of all the digits is taken)
                               3
                        371: 3  + 7  + 1  = 27 + 343 + 1 = 371 (Digit count of 371 is 3. So, cube of all the digits is taken)
                                  3
                           3
                                                                                     th
                                4
                                   4
                                       4
                            4
                        1634: 1  + 6  + 3  + 4  = 1 + 1296 + 81 + 256 = 1643 (Digit count of 1643 is 4. So, 4  power of digits is taken)
                        Some of the members of the class are given below:
                        Class name                  :   Armstrong
                        Data Members/Instance variables
                        num                         :  To store the number
                        Member Methods/Member functions
                        void getnum()               :  Accepts number in num
                        int countdigit(int a)       :  Counts and returns the number of digits in ‘a’ using the recursive technique
                                                                                        th
                        int sumpower(int a, int p)   :   Using recursive technique, returns sum of p  power of the digits of the number stored
                                                       in variable a
                        void isArmstrong()          :    Calls countdigit(int) and sumpower(int, int) and prints if the number is an Armstrong
                                                       number or not
                        Specify the class Armstrong giving details of the methods void getnum(), int countdigit(int) and int sumpower(int,int) and void
                       isArmstrong(). Also define the main() function to create an object and call the functions accordingly to enable the task.
                    2.  The Evil number is a special positive whole number that has an even number of 1’s in its binary equivalent.
                        6 is an evil number as its binary equivalent 110 has 2 1’s.
                        9 is an evil number as its binary value 1001 has 2 1’s.
                        15 is an evil number as its binary value 1111 has 4 1’s.
                        A class called Evilnum is defined to check if a number is an evil number or not. Some of the members of the class are given below:
                        Class name                  :   Evilnum
                        Data Members/Instance variables
                        num                         :  To store the number
                        Member Methods/Member functions
                        void getnum()               :  Accepts number in num from the user
                        String tobinary(int a)      :  Returns the binary equivalent of number ‘a’ as String using the recursive technique
                        int countones(String s)     :   Using recursive technique, returns the frequency of ‘1’ in its binary form ‘s’ expressed
                                                       as String
                        void isEvil()               :    Calls tobinary(int) and countones(String) and prints if the number is an evil number or not
                        Specify the class Evilnum giving details of the methods void getnum(), String tobinary(int) and int countones(String) and void
                       isEvil(). Also define the main() method to create an object and call the methods accordingly to enable the task.
                    3.  A number is said to be Krishnamurthy if the sum of factorial of its digits is equal to that number. For example, number = 145
                        = 1! + 4! + 5!  = 1 + 24 + 120 = 145
                        A class called Kmurthy is defined to check if a given number is Krishnamurthy number or not. The detail of the class is given below:
                        Class name                      :   Kmurthy
                        Data Members/instance variables
                        num                             :   To store the number
                        Member Methods/Member functions
                        void getnum()                   :   Accepts number in num from the user
                        int factorial(int a)            :  Returns the factorial of a using recursive technique




                                                                                                                       427
                                                                                                           Recursion   427
   424   425   426   427   428   429   430   431   432   433   434