2020-04-18 10:43:43 +08:00
|
|
|
#include <iostream>
|
2016-11-25 21:06:34 +08:00
|
|
|
using namespace std;
|
2019-08-21 10:10:08 +08:00
|
|
|
int fib(int n)
|
|
|
|
{
|
2019-08-29 15:49:45 +08:00
|
|
|
int res[3];
|
2019-08-21 10:10:08 +08:00
|
|
|
res[0] = 0;
|
|
|
|
res[1] = 1;
|
|
|
|
for (int i = 2; i <= n; i++)
|
|
|
|
{
|
2019-08-29 15:49:45 +08:00
|
|
|
res[2] = res[1] + res[0];
|
|
|
|
res[0] = res[1];
|
|
|
|
res[1] = res[2];
|
2016-11-25 21:06:34 +08:00
|
|
|
}
|
2019-08-29 15:49:45 +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;
|
2019-08-29 15:49:45 +08:00
|
|
|
}
|