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:
// 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 code int main()
{ extract(1234);
return 0;
} // This code is contributed by 29AjayKumar |
// Recursive function to extract // individual digit for a given // number import 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 code public static void main(String[] args)
{ extract( 1234 );
} } // This code is contributed by Rohit_ranjan |
# Recursive function to extract # individual digit for a given # number def 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 code if __name__ = = "__main__" :
extract( 1234 )
|
// Recursive function to extract // individual digit for a given // number using 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 code public static void Main(String[] args)
{ extract(1234);
} } // This code is contributed by sapnasingh4991 |
<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.