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 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 |
Java
// 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 |
Python3
# 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) |
C#
// 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 |
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.
Recommended Posts:
- Solve the Crossword Puzzle
- Decimal to Binary using recursion and without using power operator
- Recursive Practice Problems with Solutions
- Remove duplicates from a sorted linked list using recursion
- Reverse a Doubly linked list using recursion
- Sum of natural numbers using recursion
- Decimal to binary number using recursion
- Sum of digit of a number using recursion
- Binary to Gray code using recursion
- Product of 2 Numbers using Recursion
- Program for length of a string using recursion
- Print alternate nodes of a linked list using recursion
- Reversing a queue using recursion
- Leaf nodes from Preorder of a Binary Search Tree (Using Recursion)
- Generating subarrays using recursion
- Product of 2 numbers using recursion | Set 2
- Program to check if an array is palindrome or not using Recursion
- Find the value of ln(N!) using Recursion
- Delete nodes which have a greater value on right side using recursion
- Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
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.

