Finite and Infinite Recursion with examples - GeeksforGeeks

23 Jul, 2021
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 RecursionInfinite Recursion
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++
Java
Python3
C#
Javascript
// C++ program to demsonstrate Finite Recursion#include <bits/stdc++.h>using namespace std; // Recursive functionvoid 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 codeint main(){     // Initial value of N    int N = 5;     // Call the recursive function    Geek(N);    return 0;}
// Java program for the above approachclass GFG{ // Recursive functionstatic 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 codepublic static void main(String[] args){         // Initial value of N    int N = 5;     // Call the recursive function    Geek(N);}} // This code is contributed by abhinavjain194
# Python program to demsonstrate Finite Recursion# Recursive functiondef 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 NN = 5 # Call the recursive functionGeek(N) # this code is contributed by shivanisinghss2110
// C# program for the above approachusing System;using System.Collections.Generic; class GFG{ // Recursive functionstatic 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 Codepublic 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.
<script> // JavaScript program to demsonstrate Finite Recursion// Recursive functionfunction 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>
5 4 3 2 1 
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++
Java
Python3
C#
Javascript
// C++ program to demsonstrate Infinite Recursion#include <bits/stdc++.h>using namespace std; // Recursive functionvoid 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 codeint main(){     // Initial value of N    int N = 5;     // Call the recursive function    Geek(N);    return 0;}
// Java program to demsonstrate Infinite Recursionimport java.io.*;  class GFG{// Recursive functionstatic 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 codepublic 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 to demsonstrate Infinite Recursion # Recursive functiondef 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 NN = 5 # Call the recursive functionGeek(N) # This code is contributed by shivanisinghss2110
// C# program to demsonstrate Infinite Recursionusing System;  class GFG{// Recursive functionstatic 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 codepublic static void Main(String[] args)    {     // Initial value of N    int N = 5;     // Call the recursive function    Geek(N);    }} // This code is contributed by shivanisinghss2110
<script>// JavaScript program to demsonstrate Infinite Recursion// Recursive functionfunction 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>
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. 
abhinavjain194
target_2
shivanisinghss2110
GATE-CS-C-Language
C Language
C++
Recursion
Recursion
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
TCP Server-Client implementation in C
Exception Handling in C++
Multithreading in C
'this' pointer in C++
Arrow operator -> in C/C++ with Examples
Vector in C++ STL
Initialize a vector in C++ (6 different ways)
Inheritance in C++
Map in C++ Standard Template Library (STL)
C++ Classes and Objects