Sunday, 13 October 2013

Factorial of a large number in c++

#include<cmath>
#include<iostream>
#include<cstdlib>
using namespace std;
int main()
{
      unsigned int d,test;
      unsigned char *a;
      unsigned int j, n, q, z, t;
      int i;
      double p;
      cin>>n;
    n=1000;
    p = 0.0;
    for(j = 2; j <= n; j++)
        p += log10(j);
    d = (int)p + 1;
    a = new unsigned char[d];
    for (i = 1; i < d; i++)
        a[i] = 0;
    a[0] = 1;
    p = 0.0;
    for (j = 2; j <= n; j++)
    {
        q = 0;
        p += log10(j);
        z = (int)p + 1;
        for (i = 0; i <= z; i++)
        {
            t = (a[i] * j) + q;
            q = (t / 10);
            a[i] = (char)(t % 10);
        }
    }
    for( i = d -1; i >= 0;--i)
        cout << (int)a[i];
    cout<<endl;
    delete []a;
return 0;
}

No comments:

Post a Comment