Fibonacci series using Recursive Programming in C/CPP

Problem Statement:

Display the Fibonacci series for the given positive integer using recurrence relation.

Explanation:

fibonacci series using recurrence relation(image source: educative)

Fibonacci series is a series that starts with either 1 or 0 and the next number is the sum of the previous two numbers.

Eg: series starts with '0' then, 0 and 1 are starting values and the next number is 1(0+1), 2(1+1), 3(1+2), 5(2+3), 8(3+5), 13(5+8), ......so on.

In the series, there is a relation between every number which is the numbers are the "sum of the previous two numbers".

main logic to generate new number {new number = previous number + (previous number -1) }

Fn=Fn1+Fn2

Using Recurrence Relation, the value of newly generated numbers are calculated with the use of recurrence relating logic for the series limit.

Code(C/CPP):

/*Fibonacci series using recurrence relation in c/cpp*/
/*code by gowtham @starkeecode.blogspot.com*/
#include<stdio.h>
int fib(int n)
{
int result;
if(n==1 || n==2)
result=1;
else
result=fib(n-1)+fib(n-2);
return result; 
}
int main()
{
int i,n;
printf("Enter the series limit: ");
scanf("%d",&n);
for(i=1;i<=n;i++)
printf("%d ",fib(i));
}

Output:
fibonacci series using recurrence relation op
[You can mail us your suggestions and feedback,
 these are very important to us]


Post a Comment

0 Comments