Skip to content

Commit eb6b95f

Browse files
The Fibonacci sequence
1 parent 6b7eef8 commit eb6b95f

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

fibonacci_series.cpp

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
int main() {
5+
int n, t1 = 0, t2 = 1, nextTerm = 0;
6+
7+
cout << "Enter the number of terms: ";
8+
cin >> n;
9+
10+
cout << "Fibonacci Series: ";
11+
12+
for (int i = 1; i <= n; ++i) {
13+
// Prints the first two terms.
14+
if(i == 1) {
15+
cout << t1 << ", ";
16+
continue;
17+
}
18+
if(i == 2) {
19+
cout << t2 << ", ";
20+
continue;
21+
}
22+
nextTerm = t1 + t2;
23+
t1 = t2;
24+
t2 = nextTerm;
25+
26+
cout << nextTerm << ", ";
27+
}
28+
return 0;
29+
}

0 commit comments

Comments
 (0)