#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 == 0)
return 1;
else
return n*fact(n-1);
}
Enter a positive integer: 6
Factorial of 6 = 720
Explaination : In this program, we accept a positive integer from the user of whom we are going to find the factorial. The integer number is then passed in the User-defined Function named "fact". The condition is like, if the number gets equal to zero, then the it will return 1 or else it will return number times the fact of (n-1) i.e the function is calls itself. So, again the function "fact" will run for (n-1) and then like this goes on and on until the n values get 0 and it finally returns the value 1 so the functions terminates and returns that value to the main function.
2 Comment
Owsome one
BalasThankyou! Stay tune for more tutorials!
Balas