A recursive algorithm is an algorithm which calls itself with "smaller (or simpler)" input values, and which obtains the result for the current input by applying simple operations to the returned value for the smaller (or simpler) input.
1. Recursive algorithms
n!=n*(n-1)*(n-2) (n-3)........... 3*2*1
It can also be written as
for n≥0 n!= {1 if n=0
n(n-1)! Else}
We will explain recursion with a example. Consider the function for calculating the
value of n! Recursively. Here we give the number (of which the factorial is to be
found out) to the function “factorial”.
int factorial(int n)
{
if (n == 0)
return 1;
else
return (n * factorial(n-1));
}
Consider the case of input 0 and 3
When input is 0
factorial(0)=> 1
When input 3
factorial(3)
3 * factorial(2)
3 * 2 * factorial(1)
3 * 2 * 1 * factorial(0)
3*2*1*1
=> 6
In case of input 3 we can see that the function calls itself repeatedly with smaller or
simpler input values. It obtains the result for the current input by applying simple
operations to the returned value for the smaller (or simpler) input.