The first two Fibonacci numbers are 0 and 1 then next number is addition of previous two numbers.
0, 1, 1, 2, 3, 5, 8, 13, 21
0, 1, 1, 2, 3, 5, 8, 13, 21
Fibonacci number Nth can be calculated as the formular follows:
Using Recursive Method:
int fibonacci(int num)
{
if(num==0)
return 0;
if(num==1)
return 1;
return fibonacci(num-1)+fibonacci(num-2);
}
Using Iterative Method:
int fibonacci(int num)
{
int f1=0;
int f2=1;
int fn;
for(int i=2;i<n;i++)
{
fn=f1+f2;
f1=f2;
f2=fn;
}
}
Using Recursive Method:
int fibonacci(int num)
{
if(num==0)
return 0;
if(num==1)
return 1;
return fibonacci(num-1)+fibonacci(num-2);
}
Using Iterative Method:
int fibonacci(int num)
{
int f1=0;
int f2=1;
int fn;
for(int i=2;i<n;i++)
{
fn=f1+f2;
f1=f2;
f2=fn;
}
}