Page 387 - computer science (868) class 11
P. 387

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


                                                                                                                       385
                                                                                                           Recursion   385
   382   383   384   385   386   387   388   389   390   391   392