Support Forums

Full Version: [C] Factorial function
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Yes, another silly function. This time it finds the factorial of a number.

Usage:
Code:
factorial(number);

Code:
Code:
long factorial(long x)
{
    long factorial = x;
    long i = 0;
    
    for (i = (x - 1); i > 1; --i)
    {
        factorial = (factorial * i);
    }
    
    return factorial;
}

Please be aware that any input number greater than 31 will not produce a useful result. This is because long integers cannot store numbers greater than the factorial of 31. Sorry.
a better program
Code:
#include <stdio.h>
int main()
{
   int a;
   int c=0;
   int i;
   printf("enter the number for finding fact");
   scanf("%d",&a);
   for(i=1;i<=a;i++)
{
     c+=i;
     printf("the factorial is:%d",c);
}
    return 0;
}