factorial for large numbers

how to find factorial for numbers greater than 20?
the complexity of the code must be N

Use Array to Multiply,Store value as value of 21! exceeds range of data type(long long int) .

Use Basic Principle of multiplication .

See This link only after trying question yourself.

Factorials of N>20 can’t be stored even in a 64-bit long long variable.

Big integers must be used for such calculations.

Languages like Java, Python, Ruby etc. can handle big integers, but we need to write additional code in C/C++ to handle huge values.

This Image will help you understand the code

Helping Image

#include<iostream>

using namespace std;

int main()

{

    int n,j,temp;

    int arr[200];

    arr[0]=1;

    j=0;//for index of array arr

    cout<<"Enter the number.:";

    cin>>n;

    for(;n>=2;n--)

    {

        temp=0;

        for(int i=0;i<=j;i++)

        {

            temp=(arr[i]*n)+temp;

            arr[i]=temp%10;

            temp=temp/10;

        }

        while(temp>0)//for

        {

            arr[++j]=temp%10;

            temp=temp/10;

        }

    }

    for(int i=j;i>=0;i--)

    printf("%d",arr[i]);

return 0;

}
1 Like

You have to use array to store big numbers .

This codechef tutorial will be very helpful

**Link : ** https://discuss.codechef.com/questions/7349/computing-factorials-of-a-huge-number-in-cc-a-tutorial

@vijju123 I’m really curious why goyal_banna get suspended? what was his answer?