First digit in factorial of a number | JavaScript program for finding the First digit of the large factorial number ; Removing trailing 0 s as this does not change first digit . ; Loop for divide the fact until it become the single digit and return the fact ; Driver code
function firstDigit ( n ) { let fact = 1 ; for ( let i = 2 ; i <= n ; i ++ ) { fact = fact * i ; while ( fact % 10 == 0 ) fact = fact / 10 ; } while ( fact >= 10 ) fact = fact / 10 ; return ( Math . round ( fact ) ) ; } let n = 5 ; document . write ( firstDigit ( n ) ) ;