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:

  1. 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.
  2. 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 single digit
    // number, then print the
    // digit and break stop 
    // the recursion
    if(n / 10 == 0)
    {
        cout << n;
        return;
    }
  
    // If n is not a single
    // digit number, then 
    // print the last digit
    cout << n % 10 << endl;
  
    // Call the function recursively
    // for n // 10 which basically
    // calls for the remaining number
    // after removing the last digit
    return extract(n / 10);
}
  
// Driver code
int main()
{
    extract(1001);
    return 0;
}
  
// This code is contributed by 29AjayKumar

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive function to extract
// individual digit for a given 
// number
class GFG{
  
static void extract(int n)
{
      
    // If n is a single digit
    // number, then print the
    // digit and break stop 
    // the recursion
    if(n / 10 == 0)
    {
        System.out.print(n);
        return;
    }
  
    // If n is not a single
    // digit number, then 
    // print the last digit
    System.out.print(n % 10 + "\n");
  
    // Call the function recursively
    // for n // 10 which basically
    // calls for the remaining number
    // after removing the last digit
    extract(n / 10);
}
  
// Driver code
public static void main(String[] args)
{
    extract(1001);
}
}
  
// This code is contributed by Rohit_ranjan

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Recursive function to extract
# individual digit for a given
# number
def extract(n):
  
    # If n is a single digit
    # number, then print the
    # digit and break stop 
    # the recursion
    if(n//10 == 0):
        print(n)
        return
  
    # If n is not a single
    # digit number, then 
    # print the last digit
    print(n % 10)
  
    # Call the function recursively
    # for n // 10 which basically
    # calls for the remaining number
    # after removing the last digit
    return extract(n//10)
      
# Driver code
if __name__ == "__main__":
    extract(1001)

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// Recursive function to extract
// individual digit for a given 
// number
using System;
  
class GFG{
      
static void extract(int n)
{
      
    // If n is a single digit
    // number, then print the
    // digit and break stop 
    // the recursion
    if(n / 10 == 0)
    {
        Console.Write(n);
        return;
    }
  
    // If n is not a single
    // digit number, then 
    // print the last digit
    Console.Write(n % 10 + "\n");
  
    // Call the function recursively
    // for n // 10 which basically
    // calls for the remaining number
    // after removing the last digit
    extract(n / 10);
}
  
// Driver code
public static void Main(String[] args)
{
    extract(1001);
}
}
  
// This code is contributed by sapnasingh4991

chevron_right


Output:

1
0
0
1

Similar to this, various other operations can be performed using recursion. Every iterative function can be computed using the recursion.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.