Previous Next

C Recursion

Recursion is a C programming technique that calls a function on itself to find the solution to a problem.

Example:

#include <stdio.h>
int fibo(int);
int main()
{
    int terms = 12, i, n = 0;
    for (i = 1; i <= terms; i++)
    {
        printf("%d\t", fibo (n));
        n++;
    }
    return 0;
}

int fibo(int n)
{
    if (n == 0 || n == 1)
      return n;
    else
      return (fibo(n - 1) + fibo (n - 2));
}

Output

0 1 1 2 3 5 8 13 21 34 55 89
Previous Next