C Program to display Fibonacci Series using recursion

C Program to display Fibonacci Series using recursion:
If you are using any software then below program will not give an error, but if you are using TURBO C then you have to make some changes like: void main() and some function like clrscr() or getch().



#include<stdio.h>
int size; //Global variable
int fibonacci(int prev_num,int num); //Prototyping

main()
{
   static int prev_num=0,num=1;

   printf("Enter size of series: ");
   scanf("%d",&size);

   printf("1 ");
   fibonacci(prev_num,num);
}

int fibonacci(int prev_num, int num)
{
   static int i=1;
   int next_num;

   if (i == size)
      return(0);
   else
    {
      next_num = prev_num + num;
      prev_num = num;
      num = next_num;
      printf("%d\t",next_num);
      i++;
      fibonacci(prev_num,num); //Calling recursion function
    }
}

OUTPUT:

Enter size of series: 10
1    1     2       3       5       8       13      21      34      55

 

Leave a Reply

Your email address will not be published. Required fields are marked *

5 × one =

This site uses Akismet to reduce spam. Learn how your comment data is processed.