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:
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.
- Solve the Crossword Puzzle
- Decimal to Binary using recursion and without using power operator
- Reverse a stack using recursion
- Sort a stack using recursion
- 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
- Delete a linked list 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
- Generating all possible Subsequences using Recursion
- Product of 2 numbers using recursion | Set 2
- Program to check if an array is palindrome or not using Recursion