# 6 Different Types of Recursion in C Explained with Programming Example

After learning the concept of functions and how they are executed, it is a time to learn recursion.

In this tutorial, you will learn all the basic of recursion in the data structure, different types of recursion in C language and some important interview questions asked.

### What is recursion in data structure?

In simple English, recursion means repetition. And in programming languages, recursion means nesting of function calls.

In simple terms, recursion is writing a function call in its definition.

Or as Yashavant Kanetkar says,

A function is called recursive if a statement within the body of a function calls the same function.

Let us understand the concept of recursion by taking an example of the Fibonacci series.

### What is the Fibonacci series?

It is a sequence of numbers in which every next term is the sum of the previous two terms. However, this logic doesn’t apply to the first two terms of the sequence.

First two terms are initialized as 0 and 1.

The series looks like 0,1,1,2,3,5,8,13,21,……….

#### Fibonacci C Program using Loop

A program that generates Fibonacci series using for loop is given below.

It uses for loop to add the last two terms in order to calculate the next term.

#include<stdio.h> int main() { int i, first=0, second=1, next, j; printf("Fibonacci example using loop\n"); printf("Enter number of terms\n"); scanf("%d",&i); printf("Fibonacci series of %d terms is:\n",i); for(j=0; j<i; j++) { if (j<=1) next=j; else { next=first+second; first=second; second=next; } printf("%d\n",next); } return 0; }

**Output:**

Fibonacci example using loop Enter number of terms: 10 Fibonacci series of 10 terms is: 0 1 1 2 3 5 8 13 21 34

Now, the same program can be developed using the concept of recursion.

#### Fibonacci C Program using Recursion

This method involves calling the function ‘fibo’ in its own definition.

#include<stdio.h> int fibo(int); int main() { int t, n=0, m; printf("Fibonacci example using recursion\n"); printf("Enter number of terms:\n"); scanf("%d",&t); printf("Fibonacci series for %d terms is:\n",t) for(m=1; m<=t; m++) { printf("%d\n", 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:**

Fibonacci example using recursion Enter number of terms: 10 Fibonacci series of 10 terms is: 0 1 1 2 3 5 8 13 21 34

### What are the different types of Recursion in C?

#### 1. Primitive Recursion

It is the types of recursion that can be converted into a loop.

We have already seen the Fibonacci series example which can be programmed with recursion as well as with loop.

#### 2. Tail Recursion

It is a primitive recursion in which the recursive call is present as the last thing in the function.

In the above Fibonacci example, the recursive function is executed as the last statement of the ‘fibo’ function.

#### 3. Single Recursion

It is the types of recursion when there is only one recursive call in the function.

#### 4. Multiple Recursion

As by its name, it is the types of recursion when there are multiple recursive calls in the function.

It is just the opposite of a single recursion. Recursion can be either single or multiple type.

#### 5. Mutual Recursion or Indirect Recursion)

There are two or more functions involved in this type of recursion.

In this type of recursion, a function calls another function, which eventually calls the original function.

#### 6. General Recursion

If there is a function which cannot be defined without recursion, is called as general recursion.

It is the opposite of primitive type recursion.

These are the different types of recursion in C.

### Interview Questioned asked about recursion

**What is the difference between tailed and non-tailed recursion?**

In tail recursion, a recursive call is executed at the end of the function. If the recursive call is executed at the beginning or in the middle of the function, it is called as non-tailed recursion.

**How the memory is allocated to the recursive function call?**

When the recursive function gets called, the memory is allocated in stack memory. For every recursive function call, a copy of the all local variable in the function is created.

After executing the recursive function, it returns the value to the function by whom it is called and memory gets deallo0ctaed from the stack.

**What is the disadvantage of recursion?**

The memory is incremented by the number of times a recursive function is called. This is one of the drawbacks/disadvantages of recursion.

**When to (not) use recursion?**

If the memory utilization is the concern of if there is limited memory resource available, it is always a good choice implementing primitive type recursion with loops.

Mobile has very limited memory space to execute any apps. If you are developing a mobile application, avoid using recursion.

### Recursion Program for Practice

There are so many coding questions where you can implement recursion. Here is some of the basic question you can practice.

- Write a program to find the length of the string using recursion.
- Write a program to concatenate string using recursion.
- Check if the given string is a palindrome or not using recursion.

Find more interview coding questions for practice.

Some of the advance program for practicing recursion:

- Write a program to reverse the linked list element using recursion.
- Binary tree traversal using recursion.

This is all about different types of recursion in C. I am sure, this tutorial will help you understand every basic concept of it. If you any question, feel free to comment below.