Topics Index

Print Fibonacci Series

If you need explanation Read this topic

If you need Answer Take test on this topic

Write a program to print Fibonacci series with the given size.

Input (Integer) Printed Output
4 1,1,2,3,
7 1,1,2,3,5,8,13,
12 1,1,2,3,5,8,13,21,34,55,89,144,

class PrintFibanocciSeries
    public static void main(String s[])


    public static void fibanocciSeries(int size)

Topic: for Loop In Java

If you need explanation Read this topic

If you need Answer Take test on this topic

User comments below. All of them might not be correct.

fibonacci series is the series in wich each element is obtained by adding the previous two elements... i.e if 1,1, are the elements den thrd element is obtained by adding 11 =2 ..SIMILARLY FORTH ELEMENT IS 1 2=3 AND SO ON........ to print fibonacci series we will have to take loop..... step 1)# since this series starts from 1,1 hence firstly initialize two variables a=1 and b=1 and print a and b... this a and b are the frst two elemnts of series.....step 2)# now take a loop i= 1 to size-2 ... here size is the no. of elements you want to print in we have subtracted 2 from size as already we have got starting 2 elements as initial value of a and b.. step 3)# now insidethis loop take c=a  b ;this will give th next element of series.... now print this element c along wid , sop(c ",")..... now u will have to change the values of a and b to get the entire series.... now a=b and b=c. by this val of b will b given to a and b will take value of c in order to get entire series

Posted by Asma Mujtaba Khan    2014-12-05 06:29:18

hey it will be c= a plus b.... i.e adding a and b.. I dnt kno y + plus sign is not shown in comment... instead of plus sign blank spaces are showing

Posted by Asma Mujtaba Khan    2014-12-05 06:34:40

The most common method is available in websites
The most unknowm method is to take an array
1.input the array
2.we can also use the concept of recursion
3.check if a[0]=0;
4.if yes print it
5 check a[1]=1 in else if block
6.if yes print it
  intialise a loop using a so called i variable
  a[i]=a[i-1]+a[i-2]; // trick
  print untill we get sum to extent untill the conditions are met

we can call using recurision also

fib(int n)
print a[i];

interviewer is dumbstruck when i told this in oracle,,,,

Posted by Sai Veerendra    2014-12-05 08:44:15

#sai veerendra has tried diffrnt approach for fibonacci... bt if we compare both th algorithm on basis of time and space complexity den the secnd algo in wich array is used is occupying additional space i.e if we hv to print series of 1000000 elements den we will have to tk array of 100000 elements... so is it not a wastage of space.. in frst algo only 3 variables can be used to prnt any size series as values are overwrittn on same memory location....  so technically array algo is not space wise efficient..n also of fib series of recursion here time complexity equation id T(n)=T(n-1) 1.. ..

Posted by Asma Mujtaba Khan    2014-12-05 09:16:06

Happy to see a good discussion -  Asma Mujtaba Khan and Sai Veerendra. Yes it depends what we need, if we only need to print the numbers then we can Asma's, but if we any way need the output as a form of an array, then we can us Sai's. Usually recursion is considered complicated and some of the programs can be written both using loops or recursion.

Posted by Merit Campus    2014-12-06 13:37:44

This dose is now closed and the winners are Asma Mujtaba Khan,  for 'First Correct Comment', Sai Veerendra,  for 'Second Correct Comment'. The 'lucky liker' is Sai Ram. Please login into Merit Campus using facebook, to claim your recharge. Go to to raise the recharge.

Posted by Merit Campus    2014-12-10 07:55:29

© meritcampus 2019

All Rights Reserved.

Open In App