TheAlgorithms/C++ 1.0.0
All the algorithms implemented in C++
Loading...
Searching...
No Matches
fibonacci_bottom_up.cpp
1#include <iostream>
2using namespace std;
3int fib(int n) {
4 int res[3];
5 res[0] = 0;
6 res[1] = 1;
7 for (int i = 2; i <= n; i++) {
8 res[2] = res[1] + res[0];
9 res[0] = res[1];
10 res[1] = res[2];
11 }
12 return res[1];
13}
14int main(int argc, char const *argv[]) {
15 int n;
16 cout << "Enter n: ";
17 cin >> n;
18 cout << "Fibonacci number is ";
19 cout << fib(n) << endl;
20 return 0;
21}
int main()
Main function.
#define endl