Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[C] Factorial function
#1
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.
Reply


Messages In This Thread
[C] Factorial function - by Commodore - 10-13-2010, 02:10 PM
RE: [C] Factorial function - by RANA - 11-28-2010, 07:13 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  C++ stdio.h rename function problem AceInfinity 2 1,046 01-14-2012, 08:39 PM
Last Post: AceInfinity
  [C] Prime number function Commodore 2 5,090 10-11-2010, 01:11 PM
Last Post: Commodore
  Problem with a member function of a class charnet3d 2 1,012 10-31-2009, 03:22 AM
Last Post: charnet3d

Forum Jump:


Users browsing this thread: 1 Guest(s)