Skip to content

Commit 8b6bae9

Browse files
authored
Create Fibonacci.cpp
1 parent b6e2ab5 commit 8b6bae9

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

C++/Fibonacci.cpp

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

0 commit comments

Comments
 (0)