c++factorialdigitsunsigned-long-long-intlong-long

C++ count digit of factorial of a large digit, two digit number or more


Click Here to View CodeFunction to perform factorial of digit and count the number of digit of that factorial

When i give "Input:13" this code give the desired output but when i use a "Input: digit greater the this or 3 digit number" then it is unable to calculate. I think it because of the number of value unsigned long long can hold is exceeded. So is there any solution how can i find Digit of factorial for a large digit number.

///// user function code//////////////
unsigned long long int digitInFactorial(int N)
{
int count=0;
if(N==0) || (n==1)){
return 1;
}else{
int result=1;
for(int i=1;i<=N;i++){
result=result+1;}
while(result!=0)
{
result=result/10;
count=count+1;
}}
return count;
}

Solution

  • int digitsInFactorial(int N) {
        if (N < 0)
            return 0;
        
        if (N <= 1)
            return 1;
            
        double digit=0;
        
        for (int i = 1; i <= N; i++)
            digit = digit + log10(i);
    
        return floor(digit) + 1;
    }