Monday, June 18, 2012

To generate fibonacci series using recursion


/* Program to generate fibonacci series using recursion */

#include<stdio.h>

#include<conio.h>

int genFib(int n);

void main()

{

  int m,i;

  clrscr();

  printf("\n Enter the range:");

  scanf("%d",&m);

  printf("\n\nFibonacci Numbers:");

  for(i=0;i<m;i++)

  {

    printf("\n%d",genFib(i));

  }

  getch();

}

 

int genFib(int n)

{

  if(n==0)          /* Stopping Condition */

    return 0;

  else if (n==1)       /* Stopping Condition */

    return 1;

  else

    return(genFib(n-1)+genFib(n-2));      /* Recursive Call */

}

 

OUTPUT

 Enter the range:10

 

 

Fibonacci Numbers:

0

1

1

2

3

5

8

13

21

34

 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.