# C++ Program to Find Fibonacci Numbers using Dynamic Programming

«
»
This C++ Program demonstrates the the computation of Fibonacci Numbers using Dynamic Programming.

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

1. `/* `
2. ` * C++ Program to Find Fibonacci Numbers using Dynamic Programming`
3. ` */`
4. `#include <cstring>`
5. `#include <iostream>`
6. `#include <cstdlib>`
7. `#define ll long long`
8. `using namespace std;`
9. ` `
10. `ll fib = {0};`
11. `/* `
12. ` * Fibonacci Numbers using Dp`
13. ` */`
14. `ll fibo_dp(int n)`
15. `{`
16. `    fib = 1;`
17. `    fib = 1;`
18. `    if (fib[n] == 0)`
19. `    {`
20. `        for (int j = 3; j <= n; ++j)`
21. `        {`
22. `            if (fib[n] == 0)`
23. `                fib[j] = fib[j - 1] + fib[j - 2];`
24. `            else`
25. `                continue;`
26. `        }`
27. `    }`
28. `    return fib[n];`
29. `}`
30. ` `
31. `/* `
32. ` * Main`
33. ` */`
34. `int main()`
35. `{`
36. `    int n;`
37. `    while (1)`
38. `    {`
39. `        cout<<"Enter the integer n to find nth fibonnaci no.(0 to exit): ";`
40. `        cin>>n;`
41. `        if (n == 0)`
42. `            break;`
43. `        cout<<fibo_dp(n)<<endl;`
44. `    }`
45. `    return 0;`
46. `}`

```\$ g++ fibo_dp.cpp
\$ a.out

Enter the integer n to find nth fibonnaci no.(0 to exit): 10
55
Enter the integer n to find nth fibonnaci no.(0 to exit): 9
34
Enter the integer n to find nth fibonnaci no.(0 to exit): 8
21
Enter the integer n to find nth fibonnaci no.(0 to exit): 7
13
Enter the integer n to find nth fibonnaci no.(0 to exit): 6
8
Enter the integer n to find nth fibonnaci no.(0 to exit): 5
5
Enter the integer n to find nth fibonnaci no.(0 to exit): 4
3
Enter the integer n to find nth fibonnaci no.(0 to exit): 3
2
Enter the integer n to find nth fibonnaci no.(0 to exit): 2
1
Enter the integer n to find nth fibonnaci no.(0 to exit): 0

------------------
(program exited with code: 1) 