Count digits in a factorial | Set 1 | A C ++ program to find the number of digits in a factorial ; This function receives an integer n , and returns the number of digits present in n ! ; factorial exists only for n >= 0 ; base case ; else iterate through n and calculate the value ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int findDigits ( int n ) { if ( n < 0 ) return 0 ; if ( n <= 1 ) return 1 ; double digits = 0 ; for ( int i = 2 ; i <= n ; i ++ ) digits += log10 ( i ) ; return floor ( digits ) + 1 ; } int main ( ) { cout << findDigits ( 1 ) << endl ; cout << findDigits ( 5 ) << endl ; cout << findDigits ( 10 ) << endl ; cout << findDigits ( 120 ) << endl ; return 0 ; }