
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
C++ Program to Find Fibonacci Numbers using Dynamic Programming
The Fibonacci sequence is like this,
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,……
In this sequence the nth term is the sum of (n-1)th and (n-2)th terms.
To generate we can use the recursive approach, but in dynamic programming the procedure is simpler. It can store all Fibonacci numbers in a table, by using that table it can easily generate the next terms in this sequence.
Input − Take the term number as an input. Say it is 10
Output − The 10th fibinacci term is 55
Algorithm
genFiboSeries(n)
Input
max number of terms.
Output
The nth Fibonacci term.
Begin define array named fibo of size n+2 fibo[0] := 0 fibo[1] := 1 for i := 2 to n, do fibo[i] := fibo[i-1] + fibo[i-2] done return fibo[n] End
Example Code
#include<iostream> using namespace std; int genFibonacci(int n) { int fibo[n+2]; //array to store fibonacci values // 0th and 1st number of the series are 0 and 1 fibo[0] = 0; fibo[1] = 1; for (int i = 2; i <= n; i++) { fibo[i] = fibo[i-1] + fibo[i-2]; //generate ith term using previous two terms } return fibo[n]; } int main () { int n; cout << "Enter number of terms: "; cin >>n; cout << n<<" th Fibonacci Terms: "<<genFibonacci(n)<<endl; }
Output
Enter number of terms: 10 10th Fibonacci Terms: 55
- Related Articles
- C++ Program to Find Fibonacci Numbers using Recursion
- C++ Program to Find Fibonacci Numbers using Iteration
- C++ Program to Find Fibonacci Numbers using Matrix Exponentiation
- C++ Program to Find Factorial of a Number using Dynamic Programming
- C++ Program to Perform Optimal Paranthesization Using Dynamic Programming
- C++ Program to Solve Knapsack Problem Using Dynamic Programming
- Python Program to Find Longest Common Substring using Dynamic Programming with Bottom-Up Approach
- Introduction to Dynamic Programming
- Python Program for Fibonacci numbers
- Python Program to Find the Fibonacci Series Using Recursion
- Python Program to Find the Fibonacci Series without Using Recursion
- Program for Fibonacci numbers in C
- Dynamic Programming in JavaScript
- Program for Fibonacci numbers in PL/SQL
- Program to find minimum number of Fibonacci numbers to add up to n in Python?

Advertisements