-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex20.c++
61 lines (44 loc) · 846 Bytes
/
ex20.c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//@rofi
/**
20. Write a program to display the first n Fibonacci numbers using function.
*/
#include<iostream>
using namespace std;
//displays first n fibonacci numbers
//dynamic programming version(memoization...)
void fib(int n)
{
//array to store Fibonacci numbers.
int f[n+2]; // 1 extra to handle case, n = 0
int i;
//0th and 1st number of the series are 0 and 1
f[0] = 0;
f[1] = 1;
cout<<f[0]<<" "<<f[1];
for (i = 2; i < n; i++)
{
//add the previous 2 numbers
f[i] = f[i-1] + f[i-2];
cout<<" "<<f[i];
}
}
//general version
void fibo_nr(int n)
{
int f_1 = 0, f_2 = 1, c=2;
cout<<" "<<f_1<<" "<<f_2;
for(int i=2; i<n; ++i)
{
int f= f_1 + f_2;
cout<<" "<<f;
f_1=f_2;
f_2=f;
}
}
int main(int argc, char const *argv[])
{
/* code */
fibo_nr(10);
cout<<"\n";
return 0;
}