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

[Image: rana.jpg]
Reply


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

Forum Jump:


Users browsing this thread: 1 Guest(s)