Dynamic Programming Approach to Find and Print Nth Fibonacci Numbers

Consider the Recursion Tree for the 5th Fibonacci Number from the above approach:

                          fib(5)   
/ \
fib(4) fib(3)
/ \ / \
fib(3) fib(2) fib(2) fib(1)
/ \ / \ / \
fib(2) fib(1) fib(1) fib(0) fib(1) fib(0)
/ \
fib(1) fib(0)

If you see, the same method call is being done multiple times for the same value. This can be optimized with the help of Dynamic Programming. We can avoid the repeated work done in the Recursion approach by storing the Fibonacci numbers calculated so far.

Dynamic Programming Approach to Find and Print Nth Fibonacci Numbers:

Below is the implementation of the above approach: 

C++

// C++ program for Fibonacci Series
// 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

                    

C

// Fibonacci Series using Dynamic Programming
#include <stdio.h>
 
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;
}

                    

Java

// Fibonacci Series using Dynamic Programming
public class fibonacci {
    static int fib(int n)
    {
        /* Declare an array to store Fibonacci numbers. */
        int f[]
            = new int[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];
    }
 
    public static void main(String args[])
    {
        int n = 9;
        System.out.println(fib(n));
    }
};
/* This code is contributed by Rajat Mishra */

                    

Python3

# 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))

                    

C#

// C# program for Fibonacci Series
// using Dynamic Programming
using System;
class fibonacci {
 
    static int fib(int n)
    {
 
        // Declare an array to
        // store Fibonacci numbers.
        // 1 extra to handle
        // case, n = 0
        int[] f = new int[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
    public static void Main()
    {
        int n = 9;
        Console.WriteLine(fib(n));
    }
}
 
// This code is contributed by anuj_67.

                    

Javascript

<script>
 
// Fibonacci Series using Dynamic Programming
 
    function  fib(n)
    {
        /* Declare an array to store Fibonacci numbers. */
        let f = new Array(n+2); // 1 extra to handle case, n = 0
        let 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];
    }
    let n=9;
    document.write(fib(n));
     
    // This code is contributed by avanitrachhadiya2155
     
</script>

                    

PHP

<?php
//Fibonacci Series using Dynamic
// Programming
 
function fib( $n)
{
     
    /* Declare an array to store
    Fibonacci numbers. */
     
    // 1 extra to handle case,
    // n = 0
    $f = array();
    $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];
}
 
$n = 9;
echo fib($n);
 
// This code is contributed by
// anuj_67.
?>

                    

Output
34





Time complexity: O(n) for given n
Auxiliary space: O(n)

Nth Fibonacci Number

Given a number n, print n-th Fibonacci Number

The Fibonacci numbers are the numbers in the following integer sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ……..

Examples: 

Input  : n = 1

Output : 1

Input  : n = 9

Output : 34

Input  : n = 10

Output : 55

Recommended Problem
Introduction to DP
Solve Problem
 
with seed values and 
 and 
.

C++

// Fibonacci Series using Space Optimized Method
#include <bits/stdc++.h>
using namespace std;
 
int fib(int n)
{
    int a = 0, b = 1, c, i;
    if (n == 0)
        return a;
    for (i = 2; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}
 
// Driver code
int main()
{
    int n = 9;
 
    cout << fib(n);
    return 0;
}
 
// This code is contributed by Code_Mech

                    

C

// Fibonacci Series using Space Optimized Method
#include <stdio.h>
int fib(int n)
{
    int a = 0, b = 1, c, i;
    if (n == 0)
        return a;
    for (i = 2; i <= n; i++) {
        c = a + b;
        a = b;
        b = c;
    }
    return b;
}
 
int main()
{
    int n = 9;
    printf("%d", fib(n));
    getchar();
    return 0;
}

                    

Java

// Java program for Fibonacci Series using Space
// Optimized Method
public class fibonacci {
    static int fib(int n)
    {
        int a = 0, b = 1, c;
        if (n == 0)
            return a;
        for (int i = 2; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
        return b;
    }
 
    public static void main(String args[])
    {
        int n = 9;
        System.out.println(fib(n));
    }
};
 
// This code is contributed by Mihir Joshi

                    

Python3

# Function for nth fibonacci number - Space Optimisation
# Taking 1st two fibonacci numbers as 0 and 1
 
 
def fibonacci(n):
    a = 0
    b = 1
    if n < 0:
        print("Incorrect input")
    elif n == 0:
        return a
    elif n == 1:
        return b
    else:
        for i in range(2, n+1):
            c = a + b
            a = b
            b = c
        return b
 
# Driver Program
 
 
print(fibonacci(9))
 
# This code is contributed by Saket Modi

                    

C#

// C# program for Fibonacci Series
// using Space Optimized Method
using System;
 
namespace Fib {
public class GFG {
    static int Fib(int n)
    {
        int a = 0, b = 1, c = 0;
 
        // To return the first Fibonacci number
        if (n == 0)
            return a;
 
        for (int i = 2; i <= n; i++) {
            c = a + b;
            a = b;
            b = c;
        }
 
        return b;
    }
 
    // Driver function
    public static void Main(string[] args)
    {
 
        int n = 9;
        Console.Write("{0} ", Fib(n));
    }
}
}
 
// This code is contributed by Sam007.

                    

Javascript

<script>
 
// Javascript program for Fibonacci Series using Space Optimized Method
 
function fib(n)
{
    let a = 0, b = 1, c, i;
    if( n == 0)
        return a;
    for(i = 2; i <= n; i++)
    {
    c = a + b;
    a = b;
    b = c;
    }
    return b;
}
 
// Driver code
 
    let n = 9;
     
    document.write(fib(n));
 
// This code is contributed by Mayank Tyagi
 
</script>

                    

PHP

<?php
// PHP program for Fibonacci Series
// using Space Optimized Method
 
function fib( $n)
{
    $a = 0;
    $b = 1;
    $c;
    $i;
    if( $n == 0)
        return $a;
    for($i = 2; $i <= $n; $i++)
    {
        $c = $a + $b;
        $a = $b;
        $b = $c;
    }
    return $b;
}
 
// Driver Code
$n = 9;
echo fib($n);
 
// This code is contributed by anuj_67.
?>

                    

Output
34





Time Complexity: O(n) 
Auxiliary Space: O(1)

Similar Reads

Recursion Approach to Find and Print Nth Fibonacci Numbers:

...

Dynamic Programming Approach to Find and Print Nth Fibonacci Numbers:

...

Nth Power of Matrix Approach to Find and Print Nth Fibonacci Numbers

...

Contact Us