0
Write a C program for factorial of given number using recursion.
Give me answer with explanation.
1 Odpowiedź
+ 1
int fac (int n)
{
if (n < 0) return -1; //n must be positive
if (n <= 1) return 1;
return n * fac (n-1);
}
n <= 1 will be the condition to exit our recursion. Let's say n is 3.
We get 3 * fac (2), but 2 is also bigger than one, so we get
3 * 2 * fac (1). fac (1) returns 1, so the end result is 3*2*1