CPP Tutorial

CPP examples programs

CPP program to print fibonacci series

The below program prints a Fibonacci Series without recursion. The CPP cout object is used to output the result on the screen. A series is called as a Fibonacci series if the each next term of the series is a sum of previous two numbers.

Fibonacci Series:

a, b, c, d, e

 

c = a+b

d = b+c

e = c+d

 

For example,

0 2 2 4 6 10 16 26
Code

#include <iostream.h>
using namespace std;
 
int main()
{
int n = 0, a = 0, b = 2;
int c;
int mod;
 
cout << "Fibonacci series with the first 2 numbers as 0 and 2 is: ";  
cout << a << ", " << b;  
 
for (n=0; n<10; n++)  
{  
c = b + a;  
cout << ", ";
cout << c;  
 
a = b;  
b = c;  
}
return 0;
}

Output

Fibonacci series with the first 2 numbers as 0 and 2 is: 0, 2, 2, 4, 6, 10, 16, 26, 42, 68, 110, 178