Q. C Program to Print Fibonacci Series upto N terms.

Here you will find an algorithm and program in C programming language to print fibonacci series. First let us understand what fibonacci series means.

Explanation : The Fibonacci sequence is a sequence consisting of a series of numbers and each number is the sum of the previous two numbers. For Example :- 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, …….

Fibonacci Series Algorithm


START
  Step 1->Declare variables i, a, b, nextTerm
  Step 2->Initialize the variables, a=0, b=1, and nextTerm = 0
  Step 3->Initialize or Enter the number of terms of Fibonacci series to be printed
  Step 4->Repeat below steps n times
   -> print the value of a
   -> nextTerm = a + b
   -> a = b
   -> b = nextTerm
STOP


C Program to Print Fibonacci Series

#include <stdio.h> 
int fib(int n) 
{ 
  int a, b, nextTerm, i;
  a = 0;
  b = 1;
  for (i = 1; i <= n; ++i) {
        printf("%d ", a);
        nextTerm = a + b;
        a = b;
        b = nextTerm;
    }
} 
int main () 
{ 
int n = 9; 
fib(n);
return 0; 
} 

Output

0 1 1 2 3 5 8 13 21