1# Fibonacci Series using Dynamic Programming
2def fibonacci(n):
3
4 # Taking 1st two fibonacci numbers as 0 and 1
5 f = [0, 1]
6
7
8 for i in range(2, n+1):
9 f.append(f[i-1] + f[i-2])
10 return f[n]
11
12print(fibonacci(9))
13
1#include<bits/stdc++.h>
2using namespace std;
3
4class GFG{
5
6public:
7int fib(int n)
8{
9
10 // Declare an array to store
11 // Fibonacci numbers.
12 // 1 extra to handle
13 // case, n = 0
14 int f[n + 2];
15 int i;
16
17 // 0th and 1st number of the
18 // series are 0 and 1
19 f[0] = 0;
20 f[1] = 1;
21
22 for(i = 2; i <= n; i++)
23 {
24
25 //Add the previous 2 numbers
26 // in the series and store it
27 f[i] = f[i - 1] + f[i - 2];
28 }
29 return f[n];
30 }
31};
32
33// Driver code
34int main ()
35{
36 GFG g;
37 int n = 9;
38
39 cout << g.fib(n);
40 return 0;
41}
42
43// This code is contributed by SoumikMondal