Search This Blog

Loading...

Number of Digit's in N! (Factorial)

// Write a C Program to Count number of Digits in Factorial.

#include<stdio.h>
#include<math.h>

double count_digit(long a)
{
    double sum; long i;


    if(a==0)  return 1;
    else
    {
           sum=0;
      
        for(i=1;i<=a;i++)
                    sum+=log10(i);
        return floor(sum)+1;
    }
}

int main()
{
    double sum; long n;
    clrscr();
  
    while(scanf("%ld",&n)==1)
    {
        sum = count_digit(n);
        printf("%.0lf\n",sum);
    }  
    getch();
}

Input:    20 (20! = 2432902008176640000 )
Output: 19 

No comments:

Post a Comment