Finite and Infinite Recursion with examples
Last Updated :
27 Jan, 2023
The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function.
Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Traversals, DFS, etc.
Types of Recursions:
Recursion can be further classified into two kinds, depending on when they terminate:
- Finite Recursion
- Infinite Recursion
Finite Recursion:
Finite Recursion occurs when the recursion terminates after a finite number of recursive calls. A recursion terminates only when a base condition is met.
Example:
Below is an implementation to demonstrate Finite Recursion.
C++
// C++ program to demsonstrate Finite Recursion
#include <bits/stdc++.h>
using namespace std;
// Recursive function
void Geek(int N)
{
// Base condition
// When this condition is met,
// the recursion terminates
if (N == 0)
return;
// Print the current value of N
cout << N << " ";
// Call itself recursively
Geek(N - 1);
}
// Driver code
int main()
{
// Initial value of N
int N = 5;
// Call the recursive function
Geek(N);
return 0;
}
Java
// Java program for the above approach
class GFG{
// Recursive function
static void Geek(int N)
{
// Base condition
// When this condition is met,
// the recursion terminates
if (N == 0)
return;
// Print the current value of N
System.out.println(N + " ");
// Call itself recursively
Geek(N - 1);
}
// Driver code
public static void main(String[] args)
{
// Initial value of N
int N = 5;
// Call the recursive function
Geek(N);
}
}
// This code is contributed by abhinavjain194
Python3
# Python program to demsonstrate Finite Recursion
# Recursive function
def Geek( N):
# Base condition
# When this condition is met,
# the recursion terminates
if (N == 0):
return
# Pr the current value of N
print( N, end =" " )
# Call itself recursively
Geek(N - 1)
# Driver code
# Initial value of N
N = 5
# Call the recursive function
Geek(N)
# this code is contributed by shivanisinghss2110
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Recursive function
static void Geek(int N)
{
// Base condition
// When this condition is met,
// the recursion terminates
if (N == 0)
return;
// Print the current value of N
Console.Write(N + " ");
// Call itself recursively
Geek(N - 1);
}
// Driver Code
public static void Main(String[] args)
{
// Initial value of N
int N = 5;
// Call the recursive function
Geek(N);
}
}
// This code is contributed by target_2.
JavaScript
<script>
// JavaScript program to demsonstrate Finite Recursion
// Recursive function
function Geek(N)
{
// Base condition
// When this condition is met,
// the recursion terminates
if (N == 0)
return;
// Print the current value of N
document.write(N +" ");
// Call itself recursively
Geek(N - 1);
}
// Driver code
// Initial value of N
var N = 5;
// Call the recursive function
Geek(N);
// this code is contributed by shivanisinghss2110
</script>
Time Complexity: O(n)
Auxiliary Space: O(n)
The recursion tree for the above recursive function looks like this.
Recursion Tree
When the value of N becomes 0, because of the base condition, the recursion terminates.
Infinite Recursion:
Infinite Recursion occurs when the recursion does not terminate after a finite number of recursive calls. As the base condition is never met, the recursion carries on infinitely.
Example:
Below is an implementation to demonstrate Infinite Recursion.
C++
// C++ program to demsonstrate Infinite Recursion
#include <bits/stdc++.h>
using namespace std;
// Recursive function
void Geek(int N)
{
// Base condition
// This condition is never met here
if (N == 0)
return;
// Print the current value of N
cout << N << " ";
// Call itself recursively
Geek(N);
}
// Driver code
int main()
{
// Initial value of N
int N = 5;
// Call the recursive function
Geek(N);
return 0;
}
Java
// Java program to demsonstrate Infinite Recursion
import java.io.*;
class GFG
{
// Recursive function
static void Geek(int N)
{
// Base condition
// This condition is never met here
if (N == 0)
return;
// Print the current value of N
System.out.print( N +" ");
// Call itself recursively
Geek(N);
}
// Driver code
public static void main(String[] args)
{
// Initial value of N
int N = 5;
// Call the recursive function
Geek(N);
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 to demsonstrate Infinite Recursion
# Recursive function
def Geek(N):
# Base condition
# This condition is never met here
if (N == 0):
return
# Print the current value of N
print(N, end = " " )
# Call itself recursively
Geek(N)
# Driver code
# Initial value of N
N = 5
# Call the recursive function
Geek(N)
# This code is contributed by shivanisinghss2110
C#
// C# program to demsonstrate Infinite Recursion
using System;
class GFG
{
// Recursive function
static void Geek(int N)
{
// Base condition
// This condition is never met here
if (N == 0)
return;
// Print the current value of N
Console.Write( N +" ");
// Call itself recursively
Geek(N);
}
// Driver code
public static void Main(String[] args)
{
// Initial value of N
int N = 5;
// Call the recursive function
Geek(N);
}
}
// This code is contributed by shivanisinghss2110
JavaScript
<script>
// JavaScript program to demsonstrate Infinite Recursion
// Recursive function
function Geek(N)
{
// Base condition
// This condition is never met here
if (N == 0)
return;
// Print the current value of N
document.write( N +" ");
// Call itself recursively
Geek(N);
}
// Driver code
// Initial value of N
var N = 5;
// Call the recursive function
Geek(N);
// This code is contributed by shivanisinghss2110
</script>
Time Complexity: non finite as this recursion will never end.
Auxiliary Space: non finite
The recursion tree for the above recursive function looks like this.
Recursion Tree
Since the value of N never becomes 0, so the recursion never terminates. Instead, the recursion continues until the implicit stack becomes full which results in a Stack Overflow. Some compilers directly give the output as Segmentation Fault (Core Dumped), while others may abnormally terminate for some value and then show Segmentation fault.
Similar Reads
Introduction to Recursion The process in which a function calls itself directly or indirectly is called recursion and the corresponding function is called a recursive function. A recursive algorithm takes one step toward solution and then recursively call itself to further move. The algorithm stops once we reach the solution
14 min read
What is Recursion? Recursion is defined as a process which calls itself directly or indirectly and the corresponding function is called a recursive function.Example 1 : Sum of Natural Numbers Let us consider a problem to find the sum of natural numbers, there are several ways of doing that but the simplest approach is
8 min read
Difference between Recursion and Iteration A program is called recursive when an entity calls itself. A program is called iterative when there is a loop (or repetition).Example: Program to find the factorial of a number C++#include<iostream> using namespace std; // ----- Recursion ----- // method to find // factorial of given number in
5 min read
Types of Recursions 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, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inord
15+ min read
Finite and Infinite Recursion with examples The process in which a function calls itself directly or indirectly is called Recursion and the corresponding function is called a Recursive function. Using Recursion, certain problems can be solved quite easily. Examples of such problems are Towers of Hanoi (TOH), Inorder/Preorder/Postorder Tree Tr
6 min read
What is Tail Recursion Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.For example the following function print() is tail recursive.C++// An example of tail recursive funct
7 min read
What is Implicit recursion? What is Recursion? Recursion is a programming approach where a function repeats an action by calling itself, either directly or indirectly. This enables the function to continue performing the action until a particular condition is satisfied, such as when a particular value is reached or another con
5 min read
Why is Tail Recursion optimization faster than normal Recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call.Why is tail recursion optimization faster than normal recursion?In non-tail recursive functions, afte
4 min read
Recursive Functions A Recursive function can be defined as a routine that calls itself directly or indirectly. In other words, a recursive function is a function that solves a problem by solving smaller instances of the same problem. This technique is commonly used in programming to solve problems that can be broken do
4 min read
Difference Between Recursion and Induction Recursion and induction are fundamental ideas in computer science and mathematics that might be regularly used to solve problems regarding repetitive structures. Recursion is a programming technique in which a function calls itself to solve the problem, whilst induction is a mathematical proof techn
4 min read