Calculating the recursive factorial of an integer in C
The factorial of a natural number n is the product of integers less than or equal to it.n! means that we start from n and we decrement until we reach the number 1.
n! = n * (n - 1)! * (n - 2)! * ... * 1
Example
Fin
The program that calculates the factorial of an integer:
References:#include< stdio.h>
#include< stdlib.h>
//iterative
unsigned long fact (unsigned short value) {
int res = 1;
for(int i = 1; i <= value; i++) {
res *= i;
}
return res;
}
//recursive
unsigned long fact_recursive (unsigned short number)
{
if (number == 0)
return 1;
else
return number * fact_recursive(number - 1);
}
int main()
{
int number;
printf("Enter a number: ");
scanf("%d",& number);
printf("factorial of %d: %d\n",number,fact);
return 0;
}
La factorielles - wikipedia