C Program to Print Fibonacci Series – with and without using Recursion

July 27, 2020

Here is the C program to print the Fibonacci series numbers using recursion and without using recursion.

C Program to Print Fibonacci Series

Before that let us learn what is meant by the Fibonacci series and Fibonacci  number.

What is Fibonacci Series?

It is a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers.

Example:

1, 1, 2, 3, 5, 8, etc.

C Program to print Fibonacci Series without recursion

Program


#include<stdio.h>    
int main()    
{    
 int n1=0,n2=1,n3,i,num; 
 
 printf("Number of elements:");    
 scanf("%d",&num);
 
 //To print first 0, and 1.
 printf("\n%d %d",n1,n2);
 for(i=2; i < num; ++i)
 {    
  n3=n1+n2;    
  printf(" %d",n3);    
  n1=n2;    
  n2=n3;    
 }  
 
  return 0;  
 }

Output:

Number of elements:10

0 1 1 2 3 5 8 13 21 34

C Program to print Fibonacci Series using recursion

Program


 #include<stdio.h>    
 
//Function
 void my_fibonacci(int n){    
    static int n1=0,n2=1,n3;    
    if(n>0){    
         n3 = n1 + n2;    
         n1 = n2;    
         n2 = n3;    
         printf("%d ",n3);    
         my_fibonacci(n-1);    
    }    
} 
   
int main(){    
    int n;    
    printf("Number of elements: ");    
    scanf("%d",&n);    
    printf("Fibonacci Series: \n");    
    printf("%d %d ",0,1);    
    my_fibonacci(n-2);  
    return 0;  
 }    
 

Output:

Number of elements: 10
Fibonacci Series:
0 1 1 2 3 5 8 13 21 34
Tagged in:
About Author
Adeeb C (223 Articles)

Adeeb C is a Web Designer, Web Application Developer, Android App Developer, WordPress Developer, Professional Blogger. He has 5 years of experience in WordPress, Web Developing, Professional Blogging, and 2 years of experience in Android App Developing.