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.