From 9438ab1ec136b718c0f94b97f0383fd89d37ee75 Mon Sep 17 00:00:00 2001 From: asr0104 <43509800+asr0104@users.noreply.github.com> Date: Tue, 2 Oct 2018 12:36:33 +0530 Subject: [PATCH] Create fibonacci.c --- Dynamic Programming/fibonacci.c | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 Dynamic Programming/fibonacci.c diff --git a/Dynamic Programming/fibonacci.c b/Dynamic Programming/fibonacci.c new file mode 100644 index 000000000..76d995758 --- /dev/null +++ b/Dynamic Programming/fibonacci.c @@ -0,0 +1,31 @@ + +//Fibonacci Series using Dynamic Programming +#include + +int fib(int n) +{ + /* Declare an array to store Fibonacci numbers. */ + int f[n+2]; // 1 extra to handle case, n = 0 + int i; + + /* 0th and 1st number of the series are 0 and 1*/ + f[0] = 0; + f[1] = 1; + + for (i = 2; i <= n; i++) + { + /* Add the previous 2 numbers in the series + and store it */ + f[i] = f[i-1] + f[i-2]; + } + + return f[n]; +} + +int main () +{ + int n = 9; + printf("%d", fib(n)); + getchar(); + return 0; +}