From the below program, the Factorial of a number is calculated using a function called fact with a return type of integer.

1. First the main function will be called for execution.

2. fact function will be called from main function to run the code.

3. the fact function will execute and return final fact value and print from main function

 

How to calculate factorial?

3! = 3*2*1 =6

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

#include<stdio.h>
#include<math.h>
int main()
{

    printf("Enter a Number to Find Factorial: ");
    printf("\nFactorial of a Given Number is: %d ",fact());
    return 0;
}
int fact()
{
    int i,fact=1,n;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        fact=fact*i;
    }
    return fact;
}

OUTPUT:

Enter a Number to Find Factorial: 5

Factorial of a Given Number is: 120