Fibonacci Series using Dynamic Programming

This is a C++ Program that Solves Fibonacci Numbers Problem using Dynamic Programming technique.

Problem Description

Find nth fibonacci number

The Fibonacci Sequence is the series of numbers: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, …
The next number is found by adding up the two numbers before it.

Let F[i] be the ith fibonacci number

F[0]=0
F[1]=1

F[i]=F[i-1]+F[i-2]

Problem Solution
advertisement
advertisement

This problem can be solved very easily by using recursion. But this is not an efficient solution. Using DP, we can solve this problem in O(n). We will proceed in a bottom up manner and keep caching all the values to avoid recomputation.

Expected Input and Output

Case-1:

 
n=3
Expected result=2

Case-2:

Note: Join free Sanfoundry classes at Telegram or Youtube
 
n=4
Expected result=3

Case-3:

 
n=5
Expected result=5
Program/Source Code for Recursive Solution

Here is source code of the C++ Program to Solve Fibonacci Numbers Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

advertisement
  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n)
  5. {
  6.     if(n<=1)
  7.         return n;
  8.  
  9.     return fibonacci(n-1)+fibonacci(n-2);
  10. }
  11.  
  12. int main()
  13. {
  14.     int n;
  15.     cout<<"Enter the value of n"<<endl;
  16.     cin>>n;
  17.  
  18.     cout<<"Required fibonacci number is ";
  19.     cout<<fibonacci(n);
  20.  
  21.     cout<<endl;
  22.     return 0;
  23. }
Program/Source Code for DP solution

Here is source code of the C++ Program to Solve Fibonacci Numbers Problem. The C++ program is successfully compiled and run on a Linux system. The program output is also shown below.

  1. #include<iostream>
  2. using namespace std;
  3.  
  4. int fibonacci(int n)
  5. {
  6.     int F[n+1];
  7.  
  8.     F[0]=0;
  9.     F[1]=1;
  10.  
  11.     for(int i=2;i<=n;i++)
  12.         F[i]=F[i-1]+F[i-2];
  13.  
  14.     return F[n];
  15. }
  16.  
  17. int main()
  18. {
  19.     int n;
  20.     cout<<"Enter the value of n"<<endl;
  21.     cin>>n;
  22.  
  23.     cout<<"Required fibonacci number is ";
  24.     cout<<fibonacci(n);
  25.  
  26.     cout<<endl;
  27.     return 0;
  28. }
Program Explanation

In the main function, we ask the user to input the value of n. We pass this value to the function buildingProblem as parameter. This function will calculate the expected result and return it. The returned value will be displayed.

Runtime Test Cases
advertisement
 
Case-1:
$ g++ fibonacci.cpp
$ ./a.out
Enter the value of n
6
Required fibonacci number is 8

Sanfoundry Global Education & Learning Series – Dynamic Programming Problems.
To practice all Dynamic Programming Problems, here is complete set of 100+ Problems and Solutions.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.