0
3.2kviews
What are advantages and disadvantages of recursion? Comment on conditions to be considered while writing a recursive function.

Write a program to print Fibonacci series up to N terms using a recursive function.

Subject : Structured Programming Approach

Title : Functions and Parameter

Difficulty : Medium

1 Answer
0
32views

Recursion:

A function that calls itself is called as recursive function and this technique is called as recursion.

Advantages:

  1. Reduce unnecessary calling of functions.

  2. Through Recursion one can solve problems in easy way while its iterative solution is very big and complex.

  3. Extremely useful when applying the same solution.

Disadvantages:

  1. Recursive solution is always logical and it is very difficult to trace.

  2. In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return.

  3. Recursion uses more processor time

    i. A recursive function must definitely have a condition that exits from calling a function again.

    ii. Hence there must be condition that calls the function itselfif that condition is true.

    iii. If the condition is false then it will exit from the loopof calling itself again.

Program:

#include<stdio.h>

#include<conio.h>

void main()

{

    int n,i;

    int fibo(int,int,int);

    clrscr();

    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:

Enter the number of elements:10

0

1

1

2

3

5

8

13

21

34
Please log in to add an answer.