From ddab1c5c744f704f6da62e1e5a5cb59c62d41414 Mon Sep 17 00:00:00 2001 From: Moinak Banerjee Date: Tue, 1 Oct 2019 21:20:45 +0530 Subject: [PATCH] Fixes Issue #284 --- misc/FibonacciDP.c | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 misc/FibonacciDP.c diff --git a/misc/FibonacciDP.c b/misc/FibonacciDP.c new file mode 100644 index 00000000..de605f78 --- /dev/null +++ b/misc/FibonacciDP.c @@ -0,0 +1,44 @@ +//Fibonacci Series using Dynamic Programming + +/* Author: Moinak Banerjee(moinak878) + Date : 1 October ,2019 +*/ + +#include +#include + +int fib(int n) +{ + //Out of Range checking + if(n<0){ + printf("\nNo Such term !\n"); + exit(0); + } + //declaring array to store fibonacci numbers -- memoization + int f[n+2]; // one extra to handle edge case, n = 0 + int i; + + /* let 0th and 1st number of the series be 0 and 1*/ + f[0] = 0; + f[1] = 1; + + for (i = 2; i <= n; i++) + { + // Adding the previous 2 terms to make the 3rd term + f[i] = f[i-1] + f[i-2]; + } + + return f[n]; +} + +int main(){ + int number; + + //Asks for the number/position of term in Fibonnacci sequence + printf("Enter the value of n(n starts from 0 ): "); + scanf("%d", &number); + + printf("The nth term is : %d \n", fib(number)); + + return 0; +} \ No newline at end of file