Skip to content

Commit ba9f6c1

Browse files
committed
calculate fibonacci number with cpp
1 parent 746d755 commit ba9f6c1

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
#include <iostream>
2+
3+
using namespace std;
4+
5+
long fibo(long);
6+
long nonRecursiveFibo(long);
7+
8+
int main()
9+
{
10+
long input = 0;
11+
12+
cout << "enter number to calculate fibonacci non recursively : ";
13+
cin >> input;
14+
cout << nonRecursiveFibo(input) << endl;
15+
16+
cout << "enter number to calculate fibonacci recursively : ";
17+
cin >> input;
18+
cout << fibo(input) << endl;
19+
20+
return 0;
21+
}
22+
23+
long fibo(long f)
24+
{
25+
if(f<3)
26+
return 1;
27+
return fibo(f-1) + fibo(f-2);
28+
}
29+
30+
long nonRecursiveFibo(long f)
31+
{
32+
if(f<3)
33+
return 1;
34+
35+
long i = 2;
36+
long fibos[f];
37+
38+
fibos[0] = 1;
39+
fibos[1] = 1;
40+
41+
while (i<f)
42+
{
43+
fibos[i] = fibos[i-1] + fibos[i-2];
44+
i++;
45+
}
46+
47+
return fibos[f-1];
48+
}

0 commit comments

Comments
 (0)