## Factorial of a Number:

In mathematics, the**factorial**of a non negative integer

*n*, denoted by

*n*!, is the product of all positive integers less than or equal to

*n*. For example let us find factorial of 5

5! = 5*4*3*2*1=120

#### Iterative Method:

so we can simply implement a loop in iterative method.

int main()

{

int product=1,i,n;

scanf("%d",&n);

for(i=1;i<=n;i++)

{

product=product*i;

}

printf("The factorial of a number %d is %d ",n,product);

return 0;

}

#### Recursive Method:

int factorial(int x)

{

if(x==0||x==1)

return 1;

else

return x*factorial(x-1);

}

int main()

{

int n;

scanf("%d",&n);

printf("%d",factorial(n));

return 0;

}

Input: 5

Output :120

Recursive tree for factorial:

Recursive tree for factorial:

Happy Coding....

## No comments:

## Post a Comment