420 views
0 votes
0 votes
Following code prints the factorial of number .. Problem source: https://www.hackerrank.com/challenges/extra-long-factorials/problem
[Since Factorials of Number > 20 can't be stored even in a 64-bit long long variable. Big integers must be used for such calculations.]
int main(){
    int n,j=0,carry=0,a[1000]={0},i,mul,len=0;
    cin >> n;
    a[0]=1;
    for(i=2;i<=n;i++){
        for(j=0;j<=len;j++){
            mul=a[j]*i+carry;
            a[j]=mul%10;
            carry=mul/10;
            if(carry != 0 && j==len)
                len++;
        }
    }
    for(i=len;i>=0;i--){
        cout << a[i];
    }
    return 0;
}

Can anyone explain how this code is working(proceeding)?

Please log in or register to answer this question.

Related questions

0 votes
0 votes
0 answers
3
0 votes
0 votes
0 answers
4