-
Notifications
You must be signed in to change notification settings - Fork 0
/
Q#14_fibonacci.cpp
51 lines (40 loc) · 973 Bytes
/
Q#14_fibonacci.cpp
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
// ******Fibonacci******
#include <iostream>
using namespace std;
int main()
{
int n, x, y, r;
cout << "Enter the range of Fibonacci series: " << endl;
cin >> n;
// LOGIC:
/*
x + y = r
0 1 1
1 0 1
1 1 2
2 1 3
3 2 5
5 3 8
*/
/* We take 2 values x & y and initialize them to 0 & 1 respectively.
x + y will give result (r).
x will become the result (r).
y will become x.
*/
x = 0;
y = 1;
// initializing result (r) as 0 for storing sum of x & y
r = 0;
cout << "The Fibonacci Series of range " << n << " is: " << endl;
// Printing zero as default for the first value cause we are starting form 0 & 1
cout << 0 << " ";
for (int i = 1; i < n; i++)
{
r = x + y;
cout << r << " ";
// applying logic
y = x;
x = r;
}
return 0;
}