In: Computer Science
Using C++ use dynamic programming to list first 30
Fibonacci numbers.
Fibonacci sequence is famous problem solved with
recursion. However, this can also be done more efficiently using
dynamic programming. Create a program that uses dynamic programming
techniques to list the first 30 Fibonacci numbers.
Program:
#include <iostream>
using namespace std;
int main ()
{
int n = 30;
int f[n], i;
f[0] = 0;
f[1] = 1;
for(i = 2; i < n; i++)
{
f[i] = f[i - 1] + f[i - 2];
}
cout <<"\nThe First 30 fibonacci numbers: "
<< endl;
for(i = 0; i < n; i++)
{
cout << f[i] <<
endl;
}
cout << endl << endl;
return 0;
}
Screenshot: ( for reference )
Output:
-----------------
Note: If you have any doubts please comment.
It will be great help If you like.