Best 2 C Programs to Print Fibonacci Series

You are currently viewing Best 2 C Programs to Print Fibonacci Series

In this article I will discuss two probable C programs to print Fibonacci series.

Before going to the main article about C programs to print Fibonacci series, let’s learn what is Fibonacci series first.

Fibonacci series:  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.

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.

C programs to print Fibonacci series without recursion

Here we will see a C program to print Fibonacci numbers according to the need of the user without using recursion.

At first we will take the input from the user that how many Fibonacci numbers he should need.

Then the program will print first n number of Fibonacci numbers.

Let’s see the program bellow;

// C programs to print Fibonacci series without recursion

#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

The output of the above Fibonacci program will be as like as bellow.

output of C programs to print Fibonacci series

C programs to print Fibonacci series with recursion

Now we will see the same program that will print expected number of Fibonacci numbers with recursion.

The following program will take the number input and show the Fibonacci numbers accordingly.

Let’s go to the program bellow.

// C programs to print Fibonacci series 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 of above Fibonacci series program

C programs to print Fibonacci series output

However, you can also write the same code using your own logic without using function or recursion.

Try this and implement your own logic to find Fibonacci numbers according to your need.

I think you are now clear about how to print Fibonacci series using C language with recursion as well as without recursion.

I have tried my best to make this article as easy as possible. You should try this code by writing yourself.

Instead of copying others code, you should write it in your own to get the best outcome while learning programming.

If you have any opinion and thoughts about the topics, then feel free to share bellow.

Leave a Reply