Home » Freshers Interview » Fibonacci Series

Fibonacci Series

Write a program to findĀ Fibonacci Series of a function ?

let us define our funcion int fibonacci(int n) returns Fn.
If n = 0, then fibonacci() should return 0.
If n = 1, then it should return 1.
For n > 1, it should return Fn-1 + Fn-2

Let us do a dry run
n=0 –> 0
n=1 –> 1
n=2 –> 0+1=1
n=3 –> 1+1=2
n=4 –> 1+2=3
n=5 –> 2+3=5
n=6 –> 3+5=8
n=7 –> 5+8=13
n=8 –> 8+13=21
n=9 –> 13+21 =34
n=10 –> 21+32=55

A Fibonacci series looks like below
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ….

 
Iterative Solution ::

Output ::

 

Recursive Solution ::

Output::


Leave a comment

Your email address will not be published. Required fields are marked *

3 × 5 =