# C program to find factorial of the given number.

C program to find factorial of the given number.

1 Like

Program: To calculate the factorial value using recursion.

#include
int fact(int n);
int main()
{
int x, i;
printf(â€śEnter a value for x:
â€ś);
scanf(â€ť%dâ€ť, &x);
i = fact(x);
printf("
Factorial of %d is %d", x, i);
return 0;
}

int fact(int n) {
/* n=0 indicates a terminating condition /
if (n <= 0)
{
return (1);
} else
{
/
function calling itself */
return (n * fact(n - 1));
/nfact(n-1) is a recursive expression */
}
}

Output:
Enter a value for x:
4
Factorial of 4 is 24

Explanation:
fact(n) = n * fact(n-1)
If n=4
fact(4) = 4 * fact(3) there is a call to fact(3)
fact(3) = 3 * fact(2)
fact(2) = 2 * fact(1)
fact(1) = 1 * fact(0)
fact(0) = 1
fact(1) = 1 * 1 = 1
fact(2) = 2 * 1 = 2
fact(3) = 3 * 2 = 6
Thus fact(4) = 4 * 6 = 24
Terminating condition(n <= 0 here;) is a must for a recursive program. Otherwise the program enters into an infinite loop.

#include
void main()
{
int n,fact;
int factorial();
printf(â€śenter the noâ€™r whose factorial to be foundâ€ť);
scanf("%d",&n);
fact=factorial(n);
printf(â€śfactorial of the noâ€™r=%dâ€ť,fact);
}
int factorial(int x)
{
int i,f=1;
for(i=1;i<=x;i++)
{
f=f*i;
}
return(f);
}

it is the simplest format

#include

main()

{

int a,i;

long int fact;

printf("

Enter the value of a :");

scanf("%d",&amp;a);

fact=1;

i=1;

while(i&lt;=a)

{

fact=fact*i;

i=i+1;

}

printf("

Factorial= %d",fact);

}