0% found this document useful (0 votes)
54 views35 pages

Types of Recursions

This document discusses types of recursion, including: 1. Direct recursion, which includes tail recursion where the recursive call is the last statement. 2. Indirect recursion where functions call each other mutually. 3. Examples of tail recursion in various programming languages like C++, Java, Python, C#, and JavaScript are provided.

Uploaded by

lefomi7461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views35 pages

Types of Recursions

This document discusses types of recursion, including: 1. Direct recursion, which includes tail recursion where the recursive call is the last statement. 2. Indirect recursion where functions call each other mutually. 3. Examples of tail recursion in various programming languages like C++, Java, Python, C#, and JavaScript are provided.

Uploaded by

lefomi7461
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

Types of Recursions
Array Matrix Strings Hashing Linked List Stack Queue Binary Tree Binary Search Tree

Difficulty Level : Medium ● Last Updated : 27 Aug, 2021

What is Recursion?

The process in which a function calls itself directly or indirectly is called recursion and

the corresponding function is called a recursive function. Using recursive algorithm,

cer tain problems can be solved quite easily. Examples of such problems are Towers of

Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS of Graph, etc.

Types of Recursions:

Recursion are mainly of two types depending on whether a function calls itself from

within itself or more than one function call one another mutually. The first one is

called direct recursion and another one is called indirect recursion. Thus, the two

types of recursion are:

1. Direct Recursion: These can be fur ther categorized into four types:

Tail Recursion: If a recursive function calling itself and that recursive call is the last

statement in the function then it ’s known as Tail Recursion. Af ter that call the

recursive function per forms nothing. The function has to process or per form any

operation at the time of calling and it does nothing at returning time.

Example :

C++

// Code Showing Tail Recursion


#include <iostream>
Start Your Coding Journey Now!
using
We namespace
use cookies to ensurestd; Login
you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
// Recursion function Policy

https://www.geeksforgeeks.org/types-of-recursions/ 1/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

void fun(int n)
{
if (n > 0) {
cout << n << " ";

// Last statement in the function


fun(n - 1);
}
}

// Driver Code
int main()
{
int x = 3;
fun(x);
return 0;
}

// This code is contributed by shubhamsingh10

// Code Showing Tail Recursion

#include <stdio.h>

// Recursion function
void fun(int n)
{
if (n > 0) {
printf("%d ", n);

// Last statement in the function


fun(n - 1);
}
}

// Driver Code
int main()
{
int x = 3;
fun(x);
return 0;
}

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 2/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

Java

// Java code Showing Tail Recursion


class GFG {

// Recursion function
static void fun(int n)
{
if (n > 0)
{
System.out.print(n + " ");

// Last statement in the function


fun(n - 1);
}
}

// Driver Code
public static void main(String[] args)
{
int x = 3;
fun(x);
}
}

// This code is contributed by pratham76.

P ython3

# Code Showing Tail Recursion

# Recursion function
def fun(n):
if (n > 0):
print(n, end=" ")
# Last statement in the function
fun(n - 1)

# Driver Code
x = 3
fun(x)

# This code is contributed by Shubhamsingh10


Start Your Coding Journey Now! Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 3/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

C#

// C# code Showing Tail Recursion


using System;

class GFG
{

// Recursion function
static void fun(int n)
{
if (n > 0)
{
Console.Write(n + " ");

// Last statement in the function


fun(n - 1);
}
}

// Driver Code
public static void Main(string[] args)
{
int x = 3;
fun(x);
}
}

// This code is contributed by rutvik_56

Javascript

<script>
// Javascript code Showing Tail Recursion
// Recursion function
function fun(n)
{
if (n > 0)
{
document.write(n + " ");

// Last statement in the function


fun(n - 1);
Start Your Coding Journey Now!
We use Login
} cookies to ensure you have the best browsing experience on our website. By using
our}site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 4/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

// Driver Code

var x = 3;
fun(x);

// This code is contributed by shivanisinghss2110


</script>

Output :

3 2 1

Let ’s understand the example by tracing tree of recursive function. That is how the

calls are made and how the outputs are produced.

Time Complexity For Tail Recursion : O(n)

Space Complexity For Tail Recursion : O(n)

Note : Time & Space Complexity is given for this specific example. It may var y for

another example.

Lets’s now conver ting Tail Recursion into Loop and compare each other in terms of Time

& Space Complexity and decide which is more efficient.

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
C++
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 5/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

// Converting Tail Recursion into Loop


#include <iostream>
using namespace std;

void fun(int y)
{
while (y > 0) {
cout << y << " ";
y--;
}
}

// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}

//This Code is contributed by Shubhamsingh10

// Converting Tail Recursion into Loop

#include <stdio.h>

void fun(int y)
{
while (y > 0) {
printf("%d ", y);
y--;
}
}

// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Java
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 6/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

// Converting Tail Recursion into Loop


import java.io.*;
class GFG
{
static void fun(int y)
{
while (y > 0) {
System.out.print(" "+ y);
y--;
}
}

// Driver code
public static void main(String[] args)
{
int x = 3;
fun(x);

}
}

// This code is contributed by shivanisinghss2110

P ython3

# Converting Tail Recursion into Loop


def fun(y):

while (y > 0):


print(y , end = " ")
y -= 1

# Driver code
x = 3
fun(x)

# This Code is contributed by shivanisinghss2110

C#

// Converting Tail Recursion into Loop


using System;
Start Your Coding Journey Now!
class
We use cookies Login
GFG to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
{
static void fun(int y) Policy

https://www.geeksforgeeks.org/types-of-recursions/ 7/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

{
while (y > 0) {
Console.Write(" "+ y);
y--;
}
}

// Driver code
public static void Main(String[] args)
{
int x = 3;
fun(x);

}
}
// This code is contributed by shivanisinghss2110

Javascript

<script>
function fun(y)
{
while (y > 0) {
document.write(" "+ y);
y--;
}
}

// Driver code
var x = 3;
fun(x);

// This code is contributed by shivanisinghss2110


</script>

Output :

3 2 1

Start Your Coding Journey Now!


Time Complexity: O(n)

Login
We use cookies to ensure you have the best browsing experience on our website. By using Register
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Space Complexity: O(1) Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 8/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

Note : Time & Space Complexity is given for this specific example. It may var y for

another example.

So it was seen that in case of loop the Space Complexity is O(1) so it was better to write

code in loop instead of tail recursion in terms of Space Complexity which is more

efficient than tail recursion.

Why space complexity is less in case of loop ?

Before explaining this I am assuming that you are familiar with the knowledge that ’s

how the data stored in main memor y during execution of a program. In brief,when the

program executes,the main memor y divided into three par ts. One par t for code section,

the second one is heap memor y and another one is stack memor y. Remember that the

program can directly access only the stack memor y, it can’t directly access the heap

memor y so we need the help of pointer to access the heap memor y.

Let ’s now understand why space complexity is less in case of loop ?

In case of loop when function “(void fun(int y))” executes there only one activation

record created in stack memor y(activation record created for only ‘ y ’ variable) so it

takes only ‘one’ unit of memor y inside stack so it ’s space complexity is O(1) but in case

of recursive function ever y time it calls itself for each call a separate activation record

created in stack.So if there’s ‘n’ no of call then it takes ‘n’ unit of memor y inside stack so

it ’s space complexity is O(n).

Head Recursion: If a recursive function calling itself and that recursive call is the

first statement in the function then it ’s known as Head Recursion. There’s no

statement, no operation before the call. The function doesn’t have to process or

per form any operation at the time of calling and all operations are done at returning

time.

Example :

C++

// C++ program showing Head Recursion

#include <bits/stdc++.h>
using namespace std;
Start Your Coding Journey Now! Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
// Recursive function
Policy
void fun(int n)
https://www.geeksforgeeks.org/types-of-recursions/ 9/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

{
if (n > 0) {

// First statement in the function


fun(n - 1);

cout << " "<< n;


}
}

// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}

// this code is contributed by shivanisinghss2110

// C program showing Head Recursion

#include <stdio.h>

// Recursive function
void fun(int n)
{
if (n > 0) {

// First statement in the function


fun(n - 1);

printf("%d ", n);


}
}

// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
Start Your Coding Journey Now! Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 10/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

Java

// Java program showing Head Recursion


import java.io.*;

class GFG{

// Recursive function
static void fun(int n)
{
if (n > 0) {

// First statement in the function


fun(n - 1);

System.out.print(" "+ n);


}
}

// Driver code
public static void main(String[] args)
{
int x = 3;
fun(x);

}
}

// This code is contributed by shivanisinghss2110

P ython3

# Python program showing Head Recursion


# Recursive function
def fun(n):

if (n > 0):

# First statement in the function


fun(n - 1)

print(n,end=" ")

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy
# Driver code
https://www.geeksforgeeks.org/types-of-recursions/ 11/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

x = 3
fun(x)

# this code is contributed by shivanisinghss2110

C#

// Java program showing Head Recursion


using System;

class GFG{

// Recursive function
static void fun(int n)
{
if (n > 0) {

// First statement in the function


fun(n - 1);

Console.Write(" "+ n);


}
}

// Driver code
public static void Main(String[] args)
{
int x = 3;
fun(x);

}
}

// This code is contributed by shivanisinghss2110

Javascript

<script>

// JavaScript program showing Head Recursion


// Recursive function

{
Start Your Coding Journey Now!
function
We use cookies fun(n) Login
to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
if (n > 0) { Policy

https://www.geeksforgeeks.org/types-of-recursions/ 12/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

// First statement in the function


fun(n - 1);

document.write(" "+ n);


}
}

// Driver code
var x = 3;
fun(x);

// This code is contributed by shivanisinghss2110

</script>

Output :

1 2 3

Let ’s understand the example by tracing tree of recursive function. That is how the

calls are made and how the outputs are produced.

Time Complexity For Head Recursion: O(n)

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
Space Complexity For Head Recursion: O(n)

our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 13/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

Note : Time & Space Complexity is given for this specific example. It may var y for

another example.

Note : Head recursion can’t easily conver t into loop as Tail Recursion but it can. Let ’s

conver t the above code into the loop.

C++

// Converting Head Recursion into Loop


#include <iostream>
using namespace std;

// Recursive function
void fun(int n)
{
int i = 1;
while (i <= n) {
cout <<" "<< i;
i++;
}
}

// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}
// this code is contributed by shivanisinghss2110

// Converting Head Recursion into Loop

#include <stdio.h>

// Recursive function
void fun(int n)
{
int i = 1;
while (i <= n) {

Start Your Coding Journey Now!


printf("%d ", i); Login
We use cookies to ensure you have the best browsing experience on our website.
i++;
By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
} Policy

https://www.geeksforgeeks.org/types-of-recursions/ 14/35
5/24/22, 6:12 PM Types of Recursions - GeeksforGeeks

// Driver code
int main()
{
int x = 3;
fun(x);
return 0;
}

Java

// Converting Head Recursion into Loop


import java.util.*;
class GFG
{
// Recursive function
static void fun(int n)
{
int i = 1;
while (i <= n) {
System.out.print(" "+ i);
i++;
}
}

// Driver code
public static void main(String[] args)
{
int x = 3;
fun(x);
}
}

// this code is contributed by shivanisinghss2110

P ython3

# Converting Head Recursion into Loop


# Recursive function
def fun(n):

Start Your Coding Journey Now!


i cookies
We use
while
our site, (i <= n):
you acknowledge
Login
= 1 to ensure you have the best browsing experience on our website. By using
that you have read and understood our Cookie Policy & Privacy
Register
Got It !
print(i,end=" ") Policy

https://www.geeksforgeeks.org/types-of-recursions/ 15/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

i+=1

# Driver code
x = 3
fun(x)

# This code is contributed by shivanisinghss2110

C#

// Converting Head Recursion into Loop


using System;
class GFG
{
// Recursive function
static void fun(int n)
{
int i = 1;
while (i <= n) {
Console.Write(" "+ i);
i++;
}
}

// Driver code
public static void Main(String[] args)
{
int x = 3;
fun(x);
}
}

// this code is contributed by shivanisinghss2110

Javascript

<script>

// Converting Head Recursion into Loop


// Recursive function
function fun(n)
{
Start Your Coding Journey Now!
We use var
while
our site,
i =to 1;
cookies Login
ensure you have the best browsing experience on our website. By using
(i <= n)that{ you have read and understood our Cookie Policy & Privacy
you acknowledge
Register
Got It !
document.write(" "+ Policy i);

https://www.geeksforgeeks.org/types-of-recursions/ 16/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

i++;
}
}

// Driver code
var x = 3;
fun(x);

// this code is contributed by shivanisinghss2110

</script>

Output :

1 2 3

Tree Recursion: To understand Tree Recursion let ’s first understand Linear

Recursion. If a recursive function calling itself for one time then it ’s known as Linear

Recursion. Other wise if a recursive function calling itself for more than one time

then it ’s known as Tree Recursion.

Example :

Pseudo Code for linear recursion

fun(n)
{
// some code
if(n>0)
{
fun(n-1); // Calling itself only once
}
// some code
}

Program for tree recursion

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
C++
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 17/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

// C++ program to show Tree Recursion


#include <iostream>
using namespace std;

// Recursive function
void fun(int n)
{
if (n > 0)
{
cout << " " << n;

// Calling once
fun(n - 1);

// Calling twice
fun(n - 1);
}
}

// Driver code
int main()
{
fun(3);
return 0;
}

// This code is contributed by shivanisinghss2110

// C program to show Tree Recursion

#include <stdio.h>

// Recursive function
void fun(int n)
{
if (n > 0) {
printf("%d ", n);

// Calling once
fun(n - 1);

// Calling twice

Start Your Coding Journey Now!


}
fun(n
We use cookies - 1);
to ensure Login
you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
} Policy

https://www.geeksforgeeks.org/types-of-recursions/ 18/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

// Driver code
int main()
{
fun(3);
return 0;
}

Java

// Java program to show Tree Recursion


class GFG
{

// Recursive function
static void fun(int n)
{
if (n > 0) {
System.out.print(" "+ n);

// Calling once
fun(n - 1);

// Calling twice
fun(n - 1);
}
}

// Driver code
public static void main(String[] args)
{

fun(3);
}
}

// This code is contributed by shivanisinghss2110

P ython3

# C++ program to show Tree Recursion


# Recursive function
Start Your Coding Journey Now!
defusefun(n):
We Login
cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
if (n > 0): Policy

https://www.geeksforgeeks.org/types-of-recursions/ 19/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

print(n, end=" ")

# Calling once
fun(n - 1)

# Calling twice
fun(n - 1)

# Driver code
fun(3)

# This code is contributed by shivanisinghss2110

C#

// C# program to show Tree Recursion


using System;
class GFG
{

// Recursive function
static void fun(int n)
{
if (n > 0) {
Console.Write(" "+ n);

// Calling once
fun(n - 1);

// Calling twice
fun(n - 1);
}
}

// Driver code
public static void Main(String[] args)
{

fun(3);
}
}

Start Your Coding Journey Now!


our site, is contributed
acknowledge byand
that you have read shivanisinghss2110
Login
We use cookies to ensure you have the best browsing experience on our website. By using
// This youcode understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 20/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

Javascript

<script>

// JavaScript program to show Tree Recursion

// Recursive function
function fun(n)
{
if (n > 0) {
document.write(" "+ n);

// Calling once
fun(n - 1);

// Calling twice
fun(n - 1);
}
}

// Driver code
fun(3);

// This code is contributed by shivanisinghss2110

</script>

Output :

3 2 1 1 2 1 1

Let ’s understand the example by tracing tree of recursive function. That is how the

calls are made and how the outputs are produced.

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 21/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

Time Complexity For Tree Recursion: O(2^n)

Space Complexity For Tree Recursion: O(n)

Note : Time & Space Complexity is given for this specific example. It may var y for

another example.

Nested Recursion: In this recursion, a recursive function will pass the parameter as

a recursive call. That means “recursion inside recursion”. Let see the example to

understand this recursion.

Example :

C++

// C++ program to show Nested Recursion


#include <iostream>
using namespace std;

int fun(int n)
{
if (n > 100)
return n - 10;

// A recursive function passing parameter


// as a recursive call or recursion inside
// the recursion
return fun(fun(n + 11));
Start Your Coding Journey Now! Login
We use cookies to ensure you have the best browsing experience on our website. By using
}
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy
// Driver code
https://www.geeksforgeeks.org/types-of-recursions/ 22/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

int main()
{
int r;
r = fun(95);

cout << " " << r;

return 0;
}

// This code is contributed by shivanisinghss2110

// C program to show Nested Recursion

#include <stdio.h>
int fun(int n)
{
if (n > 100)
return n - 10;

// A recursive function passing parameter


// as a recursive call or recursion
// inside the recursion
return fun(fun(n + 11));
}

// Driver code
int main()
{
int r;
r = fun(95);
printf("%d\n", r);
return 0;
}

Java

// Java program to show Nested Recursion


import java.util.*;

Start Your Coding Journey Now!


class
We
static int
our site, you fun(intthatn)
acknowledge
Login
GFG {to ensure you have the best browsing experience on our website. By using
use cookies
you have read and understood our Cookie Policy & Privacy
Register
Got It !
{ Policy

https://www.geeksforgeeks.org/types-of-recursions/ 23/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

if (n > 100)
return n - 10;

// A recursive function passing parameter


// as a recursive call or recursion
// inside the recursion
return fun(fun(n + 11));
}

// Driver code
public static void main(String args[])
{
int r;
r = fun(95);
System.out.print(" "+ r);

}
}
// This code is contributed by shivanisinghss2110

P ython3

# Python program to show Nested Recursion


def fun(n):

if (n > 100):
return n - 10

# A recursive function passing parameter


# as a recursive call or recursion inside
# the recursion
return fun(fun(n + 11))

# Driver code
r = fun(95)
print("", r)

# This code is contributed by shivanisinghss2110

C#

// C# program to show Nested Recursion


Start Your Coding Journey Now!
using
We System;
use cookies Login
to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
class GFG { Policy

https://www.geeksforgeeks.org/types-of-recursions/ 24/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

static int fun(int n)


{
if (n > 100)
return n - 10;

// A recursive function passing parameter


// as a recursive call or recursion
// inside the recursion
return fun(fun(n + 11));
}

// Driver code
public static void Main(String []args)
{
int r;
r = fun(95);
Console.Write(" "+ r);

}
}
// This code is contributed by shivanisinghss2110

Javascript

<script>

// JavaScript program to show Nested Recursion


function fun( n)
{
if (n > 100)
return n - 10;

// A recursive function passing parameter


// as a recursive call or recursion
// inside the recursion
return fun(fun(n + 11));
}

// Driver code
var r;
r = fun(95);
document.write(" "+ r);

// This code is contributed by shivanisinghss2110

Start Your Coding Journey Now!


</script> Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 25/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

Output :

91

Let ’s understand the example by tracing tree of recursive function. That is how the

calls are made and how the outputs are produced.

2. Indirect Recursion: In this recursion, there may be more than one functions and they

are calling one another in a circular manner.

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 26/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

From the above diagram fun(A) is calling for fun(B), fun(B) is calling for fun(C) and

fun(C) is calling for fun(A) and thus it makes a cycle.

Example :

C++

// C++ program to show Indirect Recursion


#include <iostream>
using namespace std;

void funB(int n);

void funA(int n)
{
if (n > 0) {
cout <<" "<< n;

// Fun(A) is calling fun(B)


funB(n - 1);
}
}

void funB(int n)
{
if (n > 1) {
cout <<" "<< n;

// Fun(B) is calling fun(A)


funA(n / 2);
}
}

// Driver code
int main()
{
funA(20);
return 0;
}

// this code is contributed by shivanisinghss2110

Start Your Coding Journey Now!


C
Login
We use cookies to ensure you have the best browsing experience on our website. By using Register
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Got It !
// C program to show Indirect Recursion
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 27/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

#include <stdio.h>

void funB(int n);

void funA(int n)
{
if (n > 0) {
printf("%d ", n);

// Fun(A) is calling fun(B)


funB(n - 1);
}
}

void funB(int n)
{
if (n > 1) {
printf("%d ", n);

// Fun(B) is calling fun(A)


funA(n / 2);
}
}

// Driver code
int main()
{
funA(20);
return 0;
}

Java

// Java program to show Indirect Recursion


import java.io.*;

class GFG{

static void funA(int n)


{
if (n > 0) {
System.out.print(" " +n);

Start Your Coding Journey Now!


// toFun(A)
We use cookies
funB(n
ishave
ensure you
- 1);
calling
the best fun(B) Login
browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
} Policy

https://www.geeksforgeeks.org/types-of-recursions/ 28/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

static void funB(int n)


{
if (n > 1) {
System.out.print(" " +n);

// Fun(B) is calling fun(A)


funA(n / 2);
}
}

// Driver code
public static void main (String[] args)
{
funA(20);
}
}

// This code is contributed by shivanisinghss2110

C#

// C# program to show Indirect Recursion


using System;

class GFG{

static void funA(int n)


{
if (n > 0) {
Console.Write(" " +n);

// Fun(A) is calling fun(B)


funB(n - 1);
}
}

static void funB(int n)


{
if (n > 1) {
Console.Write(" " +n);

// Fun(B) is calling fun(A)

Start Your Coding Journey Now!


funA(n / 2);
Login
We use cookies to ensure you have the best browsing experience on our website.
}
By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
} Policy

https://www.geeksforgeeks.org/types-of-recursions/ 29/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

// Driver code
public static void Main (String[] args)
{
funA(20);
}
}

// This code is contributed by shivanisinghss2110

P ython3

# Python program to show Indirect Recursion


def funA(n):
if (n > 0):
print("", n, end='')

# Fun(A) is calling fun(B)


funB(n - 1)

def funB( n):


if (n > 1):
print("", n, end='')

# Fun(B) is calling fun(A)


funA(n // 2)

# Driver code
funA(20)

# This code is contributed by shivanisinghss2110

Javascript

<script>

// JavaScript program to show Indirect Recursion


function funA(n)
{
if (n > 0) {
document.write(n.toFixed(0) + "</br>");
Start Your Coding Journey Now!
Fun(A) that
acknowledge is you
calling
have readfun(B)
Login
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you// and understood our Cookie Policy & Privacy
Register
Got It !
funB(n - 1); Policy

https://www.geeksforgeeks.org/types-of-recursions/ 30/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

}
}

function funB(n)
{
if (n > 1) {
document.write(n.toFixed(0) + "</br>");

// Fun(B) is calling fun(A)


funA(n / 2);
}
}

// Driver code
funA(20);

// this code is contributed by shivanisinghss2110


</script>

Output :

20 19 9 8 4 3 1

Let ’s understand the example by tracing tree of recursive function. That is how the

calls are made and how the outputs are produced.

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 31/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

This ar ticle is contributed by AmiyaRanjanRout. If you like GeeksforGeeks and would

like to contribute, you can also write an ar ticle using write.geeksforgeeks.org or mail

your ar ticle to [email protected] your ar ticle appearing on the

GeeksforGeeks main page and help other Geeks.

Like 49

Previous Next

RECOMMENDED ARTICLES Page : 1 2

Different Types of Recursion in Count of unique Subsequences of


01 05
Golang given String with lengths in range
06, Jul 20
[0, N]
17, May 22

Count all possible texts that can be


02
formed from Number using given 06 Print the outer cone layer
Start
We Your
mapping
use cookies Coding
to ensure you have theJourney Now! on our website.Login
best browsing experience By using
05, May 22
Register
Got It !
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
20, May 22

Policy
Minimum of the Maximum
https://www.geeksforgeeks.org/types-of-recursions/ 32/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

distances from any node to all


Count of nodes accessible from all 07 other nodes of given Tree
03 other nodes of Graph 05, May 22

19, May 22

Sum of nodes within K distance


08
Maximum Path sum in a N-ary Tree from target
04 17, May 22 12, Apr 22

Ar ticle Contributed By :

AmiyaRanjanRout
@AmiyaRanjanRout

Vote for difficulty

Current difficulty : Medium

Easy Normal Medium Hard Expert

Improved By : AmiyaRanjanRout, pratham76, rutvik_56, darshkaushik, shivanisinghss2110,


SHUBHAMSINGH10

Article Tags : Recursion

Practice Tags : Recursion

Improve Article Report Issue

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy
Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.
https://www.geeksforgeeks.org/types-of-recursions/ 33/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks

Load Comments

5th Floor, A-118,


Sector-136, Noida, Uttar Pradesh - 201305

[email protected]

Company Learn
About Us Algorithms
Careers Data Structures
In Media SDE Cheat Sheet
Contact Us Machine learning
Privacy Policy CS Subjects
Copyright Policy Video Tutorials

News Languages
Python
Top News
Java
Technology
CPP
Work & Career
Golang
Business
C#
Finance
SQL
Lifestyle

Web Development Contribute


Web Tutorials Write an Article

Start YourDjango
CodingTutorial
Journey Now! Login
Improve an Article
We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge HTML
Register
Got It !
Pick Topics
that you have read and understood our Cookie Policy & Privacy to Write
Policy
CSS Write Interview Experience
https://www.geeksforgeeks.org/types-of-recursions/ 34/35
5/24/22, 6:13 PM Types of Recursions - GeeksforGeeks
p
JavaScript Internships
Bootstrap Video Internship

@geeksforgeeks , Some rights reserved

Start Your Coding Journey Now! Login


We use cookies to ensure you have the best browsing experience on our website. By using
our site, you acknowledge that you have read and understood our Cookie Policy & Privacy
Register
Got It !
Policy

https://www.geeksforgeeks.org/types-of-recursions/ 35/35

You might also like