Professional Documents
Culture Documents
Recursion
Recursion
is an important and powerful tool in problem solving and programming can be used as an alternative to iteration(looping)
a function is called recursive if a statement within body of that function calls the same function
a function that calls itself
if a simple case //base case or terminating condition solve it else call function with simpler version of problem
}
multiply(6,3)
18
x is 6 y is 3 3 == 1 is false ans is 6 + mulitply(6,2) return (ans)
12
int factorial(int x) //a recursive function { int ans; if(x == 0) ans = 1; else
ans = x * factorial(x-1);
return ans;
}
int main() {
result = factorial(num);
printf("The factorial of %d is %d",num, result); return 0; }