
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find Fibonacci Numbers Using Dynamic Programming in C++
In this article, we will learn how to calculate Fibonacci numbers efficiently using dynamic programming in C++. The Fibonacci sequence starts with 0 and 1, and each next number is the sum of the two preceding ones.
The basic recursive method works but becomes very slow for larger numbers because it repeats the same calculations many times. Dynamic programming solves this by storing the results of previous calculations, which makes the process much faster and more efficient.
For example, if we want to find the 6th Fibonacci number, the sequence will look like this:
F(0) = 0 F(1) = 1 F(2) = F(1) + F(0) = 1 F(3) = F(2) + F(1) = 2 F(4) = F(3) + F(2) = 3 F(5) = F(4) + F(3) = 5 F(6) = F(5) + F(4) = 8
We will show you how to write a C++ program to calculate Fibonacci numbers using dynamic programming, making the process faster and more efficient.
What is Dynamic Programming?
Dynamic Programming(DP) is a technique where we solve problems by breaking them into smaller subproblems. If a subproblem is solved multiple times, DP stores the results so that we don't have to compute them again. This makes the solution much more efficient.
We use dynamic programming when:
- A problem can be broken into smaller, overlapping subproblems.
- We can store the results of subproblems to avoid redundant work.
Finding Fibonacci number using Dynamic Programming
To calculate Fibonacci numbers using dynamic programming, follow these steps:
- First, we create an array dp[] to store the Fibonacci numbers and set the base cases: F(0) = 0 and F(1) = 1.
- Next, we fill the array using the formula F(n) = F(n-1) + F(n-2) to calculate the Fibonacci numbers from F(2) to F(n). We do this by looping through the array and using previously calculated values to compute the next Fibonacci numbers.
- Finally, after filling the array, the n-th Fibonacci number will be stored in dp[n], and we return this as the result.
Example
Here's the complete C++ example for calculating Fibonacci numbers using dynamic programming:
#include <iostream> #include <vector> int fibonacci(int n) { std::vector<int> dp(n + 1, 0); // Create a vector to store Fibonacci numbers dp[0] = 0; // Base case: Fibonacci(0) = 0 dp[1] = 1; // Base case: Fibonacci(1) = 1 // Fill the dp array with Fibonacci values for (int i = 2; i <= n; i++) { dp[i] = dp[i - 1] + dp[i - 2]; } return dp[n]; // Return the nth Fibonacci number } int main() { int n; std::cout << "Enter the position (n) to find the Fibonacci number: "; std::cin >> n; std::cout << "Fibonacci(" << n << ") = " << fibonacci(n) << std::endl; return 0; }
The output of this code will display the Fibonacci number at the specified position, like this:
Enter the position (n) to find the Fibonacci number: 7 Fibonacci(7) = 13
Conclusion
In this article, we showed how to calculate Fibonacci numbers faster using Dynamic Programming. By saving and reusing previously calculated values, we avoid repeating work and make the process quicker. This method is much faster than the simple recursive approach, especially for large n