-2

Calculate factorials in C++ by function

I wrote this code :

int fact (int A)
{
int B ;
B= A*(A-1);
return B;
}


  int main ()
{
    int x;
    cout <<"Enter number to calulate its factorial :"<<endl;
        cin >> x ;
        cout << fac (x);

}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Bsh
  • 21
  • 1
  • 6

6 Answers6

0

Have you ever tried to google it before posting there?

int factorial(int n) 
{
    if (n < 0 ) {
        return 0;
    }
    return !n ? 1 : n * factorial(n - 1);
}
Eldar Dordzhiev
  • 5,105
  • 2
  • 22
  • 26
0

Your fact function just computes factorial for one time. You should do something resursively like:

int fact (int A)
{
     if (A <= 1) {
         return 1;
     }
     return A*fact(A-1);
}

or if you want it in iterative way then you should do the following:

int fact (int A)
{
    int B = 1, i = 2;
    for (; i<=A; i++) {
        B = B*i;
    }
    return B;
}
SMA
  • 36,381
  • 8
  • 49
  • 73
  • I'd probably limit the range to unsigned integers, considering the standard factorial is limited to non-negative numbers. – Mario Nov 08 '14 at 08:01
  • @Mario I would agree with you 100%.. I just tried to rectify code that OP had so he has some basic understanding how it works. – SMA Nov 08 '14 at 08:23
0

And why din't you search it instead.

anyway...

int n, count;
    unsigned long long int factorial=1;         
    cout<<"Enter an integer: ";
    cin>>n;
    if ( n< 0)
        printf("Error!!! Factorial of negative number doesn't exist.");
    else
    {
       for(count=1;count<=n;++count)    /* for loop terminates if count>n */
       {
          factorial*=count;              /* factorial=factorial*count */
       }
    cout<<factorial;
    }
0

First of all this has nothing to do with C++ ( as your question says ). This is specific to alogorithms and they can be employed in any language.

You can use below example for your reference.

int fact (int A)
{
     if (A == 0) {
         return 1;
     }
     return A*fact(A-1);
}
ravi
  • 10,994
  • 1
  • 18
  • 36
0
int factorial (int a) {
    return a==0 ? 1 : a*factorial(a-1); 
}
Gouri
  • 347
  • 1
  • 5
-1

Since you're using C++ rather than C, I'd simply go with a template function. Bonus for this: due to expansion/implementation at compile time, your code will be highly optimized and essentially as fixed as possible with little to no overhead:

// First the generic template for pretty much all numbers
template <unsigned int X>
unsigned int factorial() {
    return X * factorial<X - 1>();
}

// Now the specialization for the special case of 0
template <>
unsigned int factorial<0>() {
    return 1;
}

For example, to calculate the factorial of 5, you'd just call factorial<5>(). With optimizations enabled, this will result in just 120. Unfortunately this is not possible with dynamic variables.

Mario
  • 35,726
  • 5
  • 62
  • 78
  • I'm not sure it is useful or appropriate, because an optimizing compiler would be able to transform the recursive (or the iterative) `fact` function -by constant folding and inlining- into a constant when its argument is constant. – Basile Starynkevitch Nov 08 '14 at 08:08
  • @BasileStarynkevitch Yeah, it probably just depends on the level of optimization. At maximum, there might not be any difference at all whether you use a template or not. – Mario Nov 08 '14 at 08:10
  • No, at least on GCC optimization does not depend if you use templates or not (since template expansion happens before optimization). So I downvote your answer. – Basile Starynkevitch Nov 08 '14 at 08:12