TheAlgorithms-C-Plus-Plus/dynamic_programming/Fibonacci_Top_Down.cpp
Christian Clauss 5c241487aa
rename Dynamic Programming -> dynamic_programming (#645)
* rename Dynamic Programming -> dynamic_programming

* rename dynamic-programming -> dynamic_programming
2019-11-28 13:29:01 +01:00

26 lines
396 B
C++

#include <bits/stdc++.h>
using namespace std;
int arr[1000000];
int fib(int n)
{
if (arr[n] == -1)
{
if (n <= 1)
arr[n] = n;
else
arr[n] = fib(n - 1) + fib(n - 2);
}
return arr[n];
}
int main(int argc, char const *argv[])
{
int n;
cout << "Enter n: ";
cin >> n;
for (int i = 0; i < n + 1; ++i)
{
arr[i] = -1;
}
cout << "Fibonacci number is " << fib(n) << endl;
return 0;
}