Support Forums
[C] Factorial function - Printable Version

+- Support Forums (https://www.supportforums.net)
+-- Forum: Categories (https://www.supportforums.net/forumdisplay.php?fid=87)
+--- Forum: Coding Support Forums (https://www.supportforums.net/forumdisplay.php?fid=18)
+---- Forum: Programming with C++ (https://www.supportforums.net/forumdisplay.php?fid=20)
+---- Thread: [C] Factorial function (/showthread.php?tid=12731)



[C] Factorial function - Commodore - 10-13-2010

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.


RE: [C] Factorial function - RANA - 11-28-2010

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;
}