TheAlgorithms-C-Plus-Plus/dynamic_programming/Fibonacci_Bottom_Up.cpp

25 lines
375 B
C++
Raw Normal View History

2016-11-25 21:06:34 +08:00
#include <bits/stdc++.h>
using namespace std;
2019-08-21 10:10:08 +08:00
int fib(int n)
{
int res[3];
2019-08-21 10:10:08 +08:00
res[0] = 0;
res[1] = 1;
for (int i = 2; i <= n; i++)
{
res[2] = res[1] + res[0];
res[0] = res[1];
res[1] = res[2];
2016-11-25 21:06:34 +08:00
}
return res[1];
2016-11-25 21:06:34 +08:00
}
int main(int argc, char const *argv[])
{
int n;
2019-08-21 10:10:08 +08:00
cout << "Enter n: ";
cin >> n;
cout << "Fibonacci number is ";
cout << fib(n) << endl;
2016-11-25 21:06:34 +08:00
return 0;
}