Find out the factorial of large number

The following program prints the factorial of large number. The program is implemented using c++ and uses Dynamic Memory!

Code :


#include<iostream>
using namespace std;
int multiply(int x, int res[], int res_size);

void factorial(int n)
{
    int *res= new int[n];
    res[0] = 1;
    int res_size = 1;
    for (int x=2; x<=n; x++)
        res_size = multiply(x, res, res_size);
    cout << "Factorial of "<<n<<" number is : ";
    for (int i=res_size-1; i>=0; i--)
        cout << res[i];
}
int multiply(int x, int res[], int res_size)
{
    int ca = 0;
    for (int i=0; i<res_size; i++)
    {
        int prod = res[i] * x + ca;
        res[i] = prod % 10;
        ca  = prod/10;
    }
    while (ca)
    {
        res[res_size] = ca%10;
        ca = ca/10;
        res_size++;
    }
    return res_size;
}

int main()
{
    int n;
    cout<<"Enter a number : ";
    cin>>n;
    factorial(n);
    return 0;
}

Output :



Comments

Popular posts from this blog

Non Restoring Division Algorithm Implementation in C

Hackerrank Modified Kaprekar Numbers Solution

Bit Stuffing Code Implementation in Java