Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

C++ Fibonacci Series

C++ Fibonacci Series

Fibonacci Series is a series in which the current element is equal to the sum of two immediate previous
elements. Fibonacci series start with 0 and 1, and progresses.

In this tutorial, we shall write C++ programs to generate Fibonacci series, and print them. We can use while
loop, do-while loop, and for loop to generate a Fibonacci Series.

Algorithm to Generate Fibonacci Series

You can use following algorithm to generate a Fibonacci Series using looping technique.

1. Start.
2. Take a variable n . We have to generate n items of Fibonacci series.
3. Create an Array fibo[] with the size of n .
4. Take index with initial value of zero.
5. Check if index is less than n . If false go to step 11.
6. If index is 0 , assign fib[index] with 0 . Go to step 9.
7. If index is 1 , assign fib[index] with 1 . Go to step 9.
8. Assign fibo[index] with the sum of previous two elements fibo[index-1] and fibo[index-
2] .
9. Increment index . Go to step 5.
10. Print fibo[] .
11. Stop.

C++ Fibonacci Series using While Loop

In the following program, we shall use C++ While Loop to generate Fibonacci Series.

C++ Program

#include <iostream>
using namespace std;

int main() {
int n = 10;
int fibo[n];
//generate fibonacci series
int index = 0;
while (index < n) {
if (index == 0)
fibo[index] = 0;
else if (index == 1)
fibo[index] = 1;
else
fibo[index] = fibo[index - 1] + fibo[index - 2];

index++;
}

//print fibonacci series


for (int i = 0; i < n; i++)
cout << fibo[i] << " ";
}

Output

0 1 1 2 3 5 8 13 21 34

C++ Fibonacci Series using Do-while Loop

In the following program, we shall use C++ Do-while Loop to generate Fibonacci Series.

C++ Program

#include <iostream>
using namespace std;

int main() {
int n = 15;
int fibo[n];

//generate fibonacci series


int index = 0;

do {
if (index == 0)
fibo[index] = 0;
else if (index == 1)
fibo[index] = 1;
else
fibo[index] = fibo[index - 1] + fibo[index - 2];

index++;
} while (index < n);

//print fibonacci series


for (int i = 0; i < n; i++)
cout << fibo[i] << " ";
}

Output
Output

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377

C++ Fibonacci Series using For Loop

In the following program, we shall use C++ For Loop to generate Fibonacci Series.

C++ Program

#include <iostream>
using namespace std;

int main() {
int n = 10;
int fibo[n];

//generate fibonacci series


for (int index = 0; index < n; index++) {
if (index == 0)
fibo[index] = 0;
else if (index == 1)
fibo[index] = 1;
else
fibo[index] = fibo[index - 1] + fibo[index - 2];
}

//print fibonacci series


for (int i = 0; i < n; i++)
cout << fibo[i] << " ";
}

Output

0 1 1 2 3 5 8 13 21 34

Conclusion

In this C++ Tutorial, we learned how to generate a Fibonacci series using looping techniques in C++.

C++ Tutorials

✦ C++ Tutorial

✦ C++ Hello World Program

✦ C++ If Else
✦ C++ Switch

✦ C++ Ternary Operator

✦ C++ Logical Operations

✦ C++ Arithmetic Operations

✦ C++ While Loop

✦ C++ Do-While Loop

✦ C++ For Loop

✦ C++ ForEach

✦ C++ Continue

✦ C++ Break

✦ C++ Comments

✦ C++ Recursion

✦ C++ Try Catch

✦ C++ String Operations

✦ C++ Array Operations

✦ C++ Vector Operations

✦ C++ Input Output Operations

✦ C++ Class

✦ C++ Programs

You might also like