Page 273 - ComputerScience_Class_11
P. 273

The output of the preceding program is as follows:

                       BlueJ: Terminal Window - Java

                   Options

                  Enter a number:
                  153
                  153 is an Armstrong Number




                   Program 3     Define a class Fibo to print the Fibonacci Series with the following specifications:
                                 Data Members
                                 int n                :   Last term of the series
                                 int a, b, c          :   Used to store values in the series
                                 Member Methods
                                 Fibo()               :   Constructor to initialise a=0,b=1,c=0 and n = 0
                                 void accept(int lt)    :    Parameterised value to store lt to n
                                 void print()         :   Displays the series up to n
                   1      import java.util.*;
                   2      class fibo

                   3      {   int n, a, b, c;
                   4          fibo()

                   5          {
                   6               a=0; b=1; c=0; n=0;
                   7          }

                   8          void accept(int lt)
                   9          {
                  10              n=lt;

                  11           }
                  12          void print()
                  13          {   int i;

                  14              if(n==1)
                  15                  System.out.print(a);
                  16              else if(n==2)

                  17                  System.out.print(a + " , " + b);
                  18              else
                  19              {

                  20                     System.out.print(a + " , " + b);
                  21                  for(i=3;i<=n;i++)
                  22                  {

                  23                         c=a+b;




                                                                                           Methods and Constructors  271
   268   269   270   271   272   273   274   275   276   277   278