Answers for "fibonacci series in c++ using dynamic programming"

C++
0

fibonacci series in c++ using dynamic programming

#include<bits/stdc++.h>
using namespace std;
 
class GFG{
     
public:
int fib(int n)
{
     
    // Declare an array to store
    // Fibonacci numbers.
    // 1 extra to handle
    // case, n = 0
    int f[n + 2];
    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];
    }
};
 
// Driver code
int main ()
{
    GFG g;
    int n = 9;
     
    cout << g.fib(n);
    return 0;
}
 
// This code is contributed by SoumikMondal
Posted by: Guest on September-25-2021
0

fibonacci series in c++ using dynamic programming

# Fibonacci Series using Dynamic Programming
def fibonacci(n):
     
    # Taking 1st two fibonacci numbers as 0 and 1
    f = [0, 1]
     
     
    for i in range(2, n+1):
        f.append(f[i-1] + f[i-2])
    return f[n]
     
print(fibonacci(9))
Posted by: Guest on September-25-2021

Code answers related to "fibonacci series in c++ using dynamic programming"

Browse Popular Code Answers by Language