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 Code
< ? php function println ( $ n , $ k ) { $ rem = 1 ; for ( $ i = 0 ; $ i < $ k ; $ i ++ ) { echo floor ( ( 10 * $ rem ) / $ n ) ; $ rem = ( 10 * $ rem ) % $ n ; } } $ n = 7 ; $ k = 3 ; println ( $ n , $ k ) ; echo " STRNEWLINE " ; $ n = 21 ; $ k = 4 ; println ( $ n , $ k ) ; ? >