C Language: Find Factorial of a Number Using Recursion

The factorial of a positive number n is given by:
factorial of n (n!) = 1*2*3*4….n

*The factorial of a negative number doesn’t exist and the factorial of 0 is 1, i.e 0! = 1

#include <stdio.h>
long int fact(int n);
int main()
{
int n;
printf(“Enter a positive integer: “);
scanf(“%d”, &n);
printf(“Factorial of %d = %ld”, n, fact(n));
return 0;
}
long int fact(int n)
{
if (n >= 1)
return n*fact(n-1);
else
return 1;
}




Output:
Enter a positive integer: 5
Factorial of 5 = 120

Translate »