A C program to find the Fibonacci series of numbers using recursion

By | October 2, 2013

Explanation of FUNCTION:

A function is a block of code that has a name and it has a property that it is reusable i.e. it can be executed from as many different points in a C Program as required.

Function groups a number of program statements into a unit and gives it a name. This unit can be invoked from other parts of a program. A computer program cannot handle all the tasks by itself. Instead its requests other program like entities – called functions in C – to get its tasks done. A function is a self contained block of statements that perform a coherent task of same kind.

The name of the function is unique in a C Program and is Global. It means that a function can be accessed from any location within a C Program. We pass information to the function called arguments specified when the function is called. And the function either returns some value to the point it was called from or returns nothing.

We can divide a long C program into small blocks which can perform a certain task. A function is a self contained block of statements that perform a coherent task of same kind.

 

Recursive function is a function which calls itself. Calling a function within itself makes it a endless loop. So we need to take care that there must be a termination condition in every recursive function.

For writing a function and using it in the C program, we should declare the function in the MAIN function. Declaration has to done in the area before the code starts, same area where we declare data variables.

void fibo_rec(int,int);

Function returns value and the ones which does not return any value is called and declared as void. This function which we are writing returns the gcd in decimal form as output. The values inside the round brackets are called Arguments. The Argument and return type are integer type. A function can return only one value so we have to take only one data type in which its value is expected.

Let’s identify variables needed for this program.
their are no variables needed in this program.

Main program needs only declaration of function prototype. print the message and call the Fibonacci FUNCTION which will print the series within the function call itself. Their is no single value to be returned hence the return value will be void.

#include<stdio.h>
void main()
{
 void fibo_rec(int,int);
 clrscr();
 printf("\nFIBONACCI SERIES < 1000 ARE :- \n");
 fibo_rec(0,1);
 getch();
}

Let’s identify variables needed for function which is part of program outside main function.
Variables which are passed in the function may not be declared again which are X & YNUM1 & NUM2 are passed to the function and saved in X & Y variable of the function.

before understanding Fibonacci recursive function. we should now Fibonacci series.

fibonacci-series

Fibonacci series is the number which is starting with 0 and 1, So the next number will be the addition of the previous two numbers Eg.  0+1 = 1, 1+1 = 2, 1+2 = 3, 2+3 = 5, 3+5 = 8 and so on.

In this program there is a risk of generating an ENDLESS LOOP. Here we have to Check the TERMINATION condition is given or not. Fibonacci series is the number which is starting with 0 and 1, So the next number will be the addition of the previous two numbers so the series generated is ENDLESS, hence the number should have a limit we are taking 1000 as the limit. In beginning print a value and call the function with the second number as first and sum of first and second as second .

void fibo_rec(int X,int Y)
{
     printf(" %d ",X);
     if(Y<1000)
    fibo_rec(Y,X+Y);
}

Note:- Remember whenever you are calling a function which is returning a value it should always be assigned to a variable of same data type or called inside the printf function as variable.

C program code :

  1. #include<stdio.h>
  2. void main()
  3. {
  4.  void fibo_rec(int,int);
  5.  clrscr();
  6.  printf("\nFIBONACCI SERIES < 1000 ARE :- \n");
  7.  fibo_rec(0,1);
  8.  getch();
  9. }
  10.  
  11. void fibo_rec(int X,int Y)
  12. {
  13.      printf(" %d ",X);
  14.      if(Y<1000)
  15.     fibo_rec(Y,X+Y);
  16. }

Screen Shots :-

C_program_Fibonacci_Recursion

C_program_Fibonacci_Recursion_Output

Leave a Reply