Problem Statement
Dynamic programming fibonacci series in c language.
Note: Top Down approach.
Solution
#include<stdio.h>
#define nill -1
#define max 100
int a[max];
int fib(int n)
{
if(a[n]==nill)
{
if(n<=1)
a[n]=n;
else
a[n]=fib(n-1)+fib(n-2);
}
return a[n];
}
int main()
{
int x;
for(int i=0;i<max;i++)
{
a[i]=nill;
}
scanf("%d",&x);
printf("%d",fib(x));
return 0;
}
Input
10
Output
55
Program Explanation:
- In this we use dynamic programming concept for solving fibonacci series using c language. This approach is known as top down approach(Memoization).
- Initially we declare the integer array with max of 100, then using defined variable nill we assign it to the integer array with the help of for loop.
- After getting the input from user we calling the function known as fib (name we given).
- After processing the input using dynmaic programming approach known as top down it will return the final output of fibonacci series.
- The result of fibonaaci series will be displayed in printf function.