Print first k digits of 1 / n where n is a positive integer | Function to print first k digits after dot in value of 1 / n . n is assumed to be a positive integer . ; Initialize remainder ; Run a loop k times to print k digits ; The next digit can always be obtained as doing ( 10 * rem ) / 10 ; Update remainder ; Driver program to test above function
function print ( n , k ) { let rem = 1 ; let ans = ' ' ; for ( let i = 0 ; i < k ; i ++ ) { ans += Math . floor ( ( ( 10 * rem ) / n ) ) ; rem = ( 10 * rem ) % n ; } document . write ( ans ) } let n = 7 ; let k = 3 ; print ( n , k ) ; document . write ( " " ) ; n = 21 ; k = 4 ; print ( n , k ) ;