Page 221 - computer science (868) class 11
P. 221
12 void print()
13 { int i;
14 if(n==1)
15 System.out.print(a);
16 else
17 if(n==2)
18 System.out.print(a + " , " + b);
19 else
20 {
21 System.out.print(a + " , " + b);
22 for(i=3;i<=n;i++)
23 {
24 c=a+b;
25 System.out.print(" , " + c);
26 a=b;
27 b=c;
28 }
29 }
30 }
31 public static void main()
32 {
33 Scanner sc= new Scanner(System.in);
34 int n;
35 System.out.println("Enter the last term of the Fibonacci series : ");
36 n= sc.nextInt();
37 fibo ob = new fibo();
38 ob.accept(n);
39 ob.print();
40 }
41 }
The output of the preceding program is as follows:
Enter the last term of the Fibonacci series :
5
0 , 1 , 1 , 2 , 3
219
Methods and Constructors 219

