How to solve problems related to Number-Digits using 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 a recursive algorithm, certain problems can be solved quite easily. A method to solve the number digit problems using recursion is discussed in this article.Â
Two main components exist for any recursive function are:Â
Â
- Base Case: A base case is a condition which stops the recursive function calls. A recursive function cannot be formed without a base case because the stack overflow error occurs when the base case is not defined as the function will keep on repeatedly calling itself. For a recursive solution, there can be more than one base case.
- Recursive Case: For all the other conditions apart from the base cases, the function calls itself with a new set of values such that after some finite recursive calls, the function finally calls for a base case and stops itself.
Let’s visualize the recursion by extracting individual digits from a given number. This is the basic step in performing many other mathematical operations.Â
Below is the implementation to extract every individual digit of a number:Â
Â
C++
// Recursive function to extract// individual digit for a given// number#include<bits/stdc++.h>using namespace std;Â
void extract(int n){Â
    // If n is a zero    // then, stop the recursion    if(n == 0)    {        return;    }Â
       // Call the function recursively    // for n // 10 which basically    // calls for the remaining number    // after removing the last digit    extract(n / 10);           // print the current last digit of the number      // with n%10;    cout << n % 10 << endl;Â
}Â
// Driver codeint main(){Â Â Â Â extract(1234);Â Â Â Â return 0;}Â
// This code is contributed by 29AjayKumar |
Java
// Recursive function to extract// individual digit for a given // numberimport java.io.*;import java.util.*;class GFG{Â
static void extract(int n){         // If n is a zero    // then, stop the recursion    if(n == 0)    {        return;    }Â
    // Call the function recursively    // for n/10 which basically    // calls for the remaining number    // after removing the last digit    extract(n / 10);         // print the current last digit of the number      // with n%10;      System.out.println(n%10);}Â
// Driver codepublic static void main(String[] args){Â Â Â Â extract(1234);}}Â
// This code is contributed by Rohit_ranjan |
Python3
# Recursive function to extract# individual digit for a given# numberdef extract(n):Â
    # If n is a zero    # the stop the recursion    if(n == 0):        returnÂ
    # Call the function recursively    # for n // 10 which basically    # calls for the remaining number    # after removing the last digit    extract(n//10)         # print the last digit with n%10    print(n % 10)Â
     # Driver codeif __name__ == "__main__":    extract(1234) |
C#
// Recursive function to extract// individual digit for a given // numberusing System;Â
class GFG{     static void extract(int n){        // If n is a zero   // then, stop the recursion    if(n == 0)    {        return;    }Â
    // Call the function recursively    // for n // 10 which basically    // calls for the remaining number    // after removing the last digit    extract(n / 10);         // print the current last digit of the number      // with n%10;    Console.Write(n % 10 + "\n");Â
}Â
// Driver codepublic static void Main(String[] args){Â Â Â Â extract(1234);}}Â
// This code is contributed by sapnasingh4991 |
Javascript
<script>Â
    // Recursive function to extract    // individual digit for a given number         function extract(n)    {Â
        // If n is a zero        // then stop the recursion        if(parseInt(n) == 0)        {            return;        }                 // Call the function recursively        // for n // 10 which basically        // calls for the remaining number        // after removing the last digit        extract(parseInt(n / 10, 10));                 // print the current last digit of the number          // with n%10;        document.write(n % 10 + "</br>");Â
    }         extract(1001);Â
</script> |
1 2 3 4
Similar to this, various other operations can be performed using recursion. Every iterative function can be computed using the recursion.Â
Â
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 zambiatek!



