Fibonacci series in C

Fibonacci series is one kind of series where the next number will be equal to the sum of previous two numbers. First two numbers of a fibonacci series will be 0 and 1. Then the other number will be the result of sum to previous two numbers.

fibonacci series in c, c program to print fibonacci series, fibonacci series

First few numbers of a fibonacci series will be 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ……etc. Look, here first two numbers are 0 and 1. But other numbers are equal to the result of addition of previous two numbers.

We can write a program to print fibonacci series in C in various way. Let’s see some way to print a fibonacci series in C programming.

Fibonacci series program in C without recursion

// fibonacci series program in C
#include <stdio.h>
int main(){
  int num, first = 0, second = 1, other, i;

  printf("Enter total number in the series : ");
  scanf("%d", &num);

  printf("\nFirst %d numbers of Fibonacci series are : \n\n", num);

  for (i = 0; i < num; i++){
    if (i <= 1){
      other = i;
    }else{
      other = first + second;
      first = second;
      second = other;
    }
    printf("%d ", other);
  }
  printf("\n");
  return 0;
}

Output of this fibonacci series program:

Fibonacci series program in C with recursion

// fibonacci series program in C with recursion
#include <stdio.h>
int fibo(int num){     // function for fibonacci series
  if (num == 0 || num == 1){
    return num;
  }else{
    return (fibo(num - 1) + fibo(num - 2));
  }
}

int main(){
  int num, i, k = 0;
  printf("Enter how many numbers you want in the series : ");
  scanf("%d", &num);

  printf("Your expected fibonacci series is :\n");

  for (i = 1; i <= num; i++){
    printf("%d ", fibo(k));
    k++;
  }
  return 0;
}

Output:

Previous pageFind factorial program

Next pageFibonacci triangle