Types of Recursion in C - Javatpoint
Types of Recursion in C - Javatpoint
Home C C++ C# Java SQL HTML CSS JavaScript XML Ajax Android Cloud
⇧ SCROLL TO TOP
Types of Recursion in C
This section will discuss the different types of recursion in the C programming language. Recursion is the process in
which a function calls itself up to n-number of times. If a program allows the user to call a function inside the same
function recursively, the procedure is called a recursive call of the function. Furthermore, a recursive function can call
itself directly or indirectly in the same program.
void recursion ()
{
recursion(); // The recursive function calls itself inside the same function
}
int main ()
{
recursion (); // function call
}
In the above syntax, the main() function calls the recursion function only once. After that, the recursion function calls
itself up to the defined condition, and if the user doesn't define the condition, it calls the same function infinite
times.
⇧ SCROLL TO TOP
1. Direct Recursion
2. Indirect Recursion
3. Tail Recursion
5. Linear recursion
6. Tree Recursion
Direct Recursion
When a function calls itself within the same function repeatedly, it is called the direct recursion.
fun()
{
// write some code
fun();
// some code
}
In the above structure of the direct recursion, the outer fun() function recursively calls the inner fun() function, and
this type of recursion is called the direct recursion.
Program2.c
#include<stdio.h>
int fibo_num (int i)
{
// if the num i is equal to 0, return 0;
if ( i == 0)
{
return 0;
}
if ( i == 1)
⇧ SCROLL TO TOP
{
https://www.javatpoint.com/types-of-recursion-in-c#:~:text=Different types of the recursion&text=Direct Recursion,No Tail%2F Head Recursion 3/13
5/24/22, 6:46 PM Types of Recursion in C - javatpoint
return 1;
}
return fibo_num (i - 1) + fibonacci (i -2);
}
int main ()
{
int i;
// use for loop to get the first 10 fibonacci series
for ( i = 0; i < 10; i++)
{
printf (" %d \t ", fibo_num (i));
}
return 0;
}
Output
0 1 1 2 3 5 8 13 21 34
Indirect Recursion
When a function is mutually called by another function in a circular manner, the function is called an indirect
recursion function.
fun1()
{
// write some code
fun2()
}
⇧ SCROLL TO TOP
fun2()
{
// write some code
fun3()
// write some code
}
fun3()
{
// write some code
fun1()
}
In this structure, there are four functions, fun1(), fun2(), fun3() and fun4(). When the fun1() function is executed, it
calls the fun2() for its execution. And then, the fun2() function starts its execution calls the fun3() function. In this
way, each function leads to another function to makes their execution circularly. And this type of approach is called
indirect recursion.
Program3.c
#include <stdio.h>
// declaration of the odd and even() function
void odd(); // Add 1 when the function is odd()
void even(); // Subtract 1 when the function is even
int num = 1; // global variable
void odd ()
{
// if statement check and execute the block till n is less than equal to 10
if (num <= 10)
{
printf (" %d ", num + 1); // print a number by adding 1
num++; // increment by 1
even(); // invoke the even function
}
return;
}
void even ()
{
// if block check the condition that n is less than equal to 10
if ( num
⇧ SCROLL TO<= 10)
TOP
{
printf (" %d ", num - 1); // print a number by subtracting 1
num++;
odd(); // call the odd() function
}
return;
}
int main ()
{
odd(); // main call the odd() function at once
return 0;
}
Output
2 1 4 3 6 5 8 7 10 9
Tail Recursion
A recursive function is called the tail-recursive if the function makes recursive calling itself, and that recursive call is
the last statement executes by the function. After that, there is no function or statement is left to call the recursive
function.
Program4.c
#include <stdio.h>
// function definition
void fun1( int num)
⇧ SCROLL
{ TO TOP
Output
Number is: 7
Number is: 6
Number is: 5
Number is: 4
Number is: 3
Number is: 2
Number is: 1
A function is called the non-tail or head recursive if a function makes a recursive call itself, the recursive call will be
the first statement in the function. It means there should be no statement or operation is called before the recursive
calls. Furthermore, the head recursive does not perform any operation at the time of recursive calling. Instead, all
operations are done at the return time.
Program5.c
#include <stdio.h>
void head_fun (int num)
{
⇧ SCROLL
if ( num >TO
0 )TOP
{
// Here the head_fun() is the first statement to be called
head_fun (num -1);
printf (" %d", num);
}
}
int main ()
{
int a = 5;
printf (" Use of Non-Tail/Head Recursive function \n");
head_fun (a); // function calling
return 0;
}
Output
Linear Recursion
A function is called the linear recursive if the function makes a single call to itself at each time the function runs and
grows linearly in proportion to the size of the problem.
Program6.c
#include <stdio.h>
#define NUM 7
int rec_num( int *arr, int n)
{
if (n == 1)
{
return arr[0];
}
return Max_num (rec_num (arr, n-1), arr[n-1]);
}
// get the maximum number
int Max_num (int n, int m)
⇧ SCROLL
{ TO TOP
if (n > m)
return n;
return m;
}
int main ()
{
// declare and initialize an array
int arr[NUM] = { 4, 8, 23, 19, 5, 35, 2};
int max = rec_num(arr, NUM); // call function
printf (" The maximum number is: %d\n", max); // print the largest number
}
Output
Tree Recursion
A function is called the tree recursion, in which the function makes more than one call to itself within the recursive
function.
Program7.c
#include <stdio.h>
// It is called multiple times inside the fibo_num function
int fibo_num (int num)
{
if (num <= 1)
⇧ SCROLL
returnTO TOP
num;
Output
← Prev Next →
Feedback
Handmade in India
React Native Python Design Python Pillow Python Turtle Keras tutorial
tutorial Patterns tutorial tutorial
Keras
React Native Python Design Python Pillow Python Turtle
Patterns
Preparation
Trending
⇧ SCROLL TO Technologies
TOP
Machine DevOps
Learning Tutorial Tutorial
Machine Learning DevOps
B.Tech / MCA
Software
Engineering
C++ tutorial Java tutorial .Net Python tutorial List of
Framework Programs
C++ Java Python
tutorial
Programs
.Net
⇧ SCROLL TO TOP