Page 413 - Cs_withBlue_J_C11_Flipbook
P. 413

23              {
                   24                  System.out.print(generate(i-1)+" ");

                   25              }
                   26          }

                   27          public static void main(int a)
                   28          {

                   29              Fibo ob=new Fibo();
                   30              ob.getnum(a);
                   31              ob.print();

                   32          }

                   33      }

                 The output of the preceding program is as follows:













                 First 8 terms of a Fibonacci series are
                 0 1 1 2 3 5 8 13


                   Program 2     Design a class Prime to check if a given number is a prime number or not. [A number is said
                                 to be prime if it is divisible by 1 and itself].
                                 Example: 3, 5, 7, 11, 13, 17, 19, 23, 29, etc. Some of the members of the class are given below:
                                 Class name            :   Prime
                                 Data Members/Instance variables
                                 num                   :   To store the number
                                 Methods/Member functions
                                 Prime (int nn)        :   Parameterised constructor to initialise the data member num=nn
                                 int countfactors(int i)  :     Returns the count of the factors of the number(num), using a
                                                           recursive technique
                                 void check()          :     Checks whether  the given number  is Prime  by invoking  the
                                                           function  countfactors(int)  and  displays  the  result  with  an
                                                           appropriate message
                                 Specify the class Prime giving details of the constructor, int countfactors(int) and void check().
                                 Define a main() function to create an object and call the functions accordingly to enable the task.

                   1       class Prime

                   2       {
                   3           int num; // data members

                   4           // parameterised constructor


                                                                                                                       411
                                                                                                           Recursion   411
   408   409   410   411   412   413   414   415   416   417   418