0
1.9kviews
Explain recursion concept. Write a program to display Fibonacci series using recursion.
1 Answer
0
24views

Recursive Function:

  • A function that calls itself is called as recursive function.

  • A recursive function must definitely have a condition that exists from calling the function again.

  • Hence there must be a condition that calls the function itself if that condition is true.

  • If the condition is false then it will exit from the loop of calling itself again.

Algorithm:

Main () Function:

  • Start.
  • Enter a number to be printed.
  • Input n.
  • Print “0”.
  • Set i = 1.
  • If i > n-1, then goto step 11
  • X = CALL fibo (argument: 0, 1, i)
  • Print x.
  • Set i = i + 1.
  • Goto step 6.
  • Stop.

Fibo(parameters : a, b, i)

  • Start.
  • If I = 1, then return b.
  • Else return (CALL (fibo(arguments: b, a + b, i-1))
  • Stop.

Program:

#include <stdio.h>
#include <conio.h>
int main(){
    int n, i;
    int fibo (int, int, int);

    printf("Enter the number of Elements:");
    scanf("%d", &n);

    printf("0\n");

    for(i=1; i<n-1; i++)    {
        printf("%d\n", fibo(0, 1, i));
    }
    getch();
}
int fibo(int a, int b, int i){
    if(i==1)
    return b;
    else
    return (fibo(b, a+b, --i));
}

Output:

How Many Numbers: 10

0

1

1

2

3

5

8

13

21

34

Please log in to add an answer.