-
Notifications
You must be signed in to change notification settings - Fork 140
Expand file tree
/
Copy pathFibonacci.cpp
More file actions
38 lines (30 loc) · 784 Bytes
/
Fibonacci.cpp
File metadata and controls
38 lines (30 loc) · 784 Bytes
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
#include <iostream>
void fibonacci_series(int n) {
int t1 = 0, t2 = 1, nextTerm = 0;
std::cout << "Fibonacci Series up to " << n << " terms: ";
for (int i = 1; i <= n; ++i) {
// Prints the first two terms.
if(i == 1) {
std::cout << t1 << ", ";
continue;
}
if(i == 2) {
std::cout << t2 << ", ";
continue;
}
// Calculates the next term
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
std::cout << nextTerm;
// Add a comma and space unless it is the last term
if (i < n) {
std::cout << ", ";
}
}
}
int main() {
int terms = 10; // Number of terms to print
fibonacci_series(terms);
return 0;
}