Print first k digits of 1 / n where n is a positive integer | Python code to 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
import math NEW_LINE def Print ( n , k ) : NEW_LINE INDENT rem = 1 NEW_LINE for i in range ( 0 , k ) : NEW_LINE INDENT print ( math . floor ( ( ( 10 * rem ) / n ) ) , end = " " ) NEW_LINE rem = ( 10 * rem ) % n NEW_LINE DEDENT DEDENT n = 7 NEW_LINE k = 3 NEW_LINE Print ( n , k ) ; NEW_LINE print ( " ▁ " ) NEW_LINE n = 21 NEW_LINE k = 4 NEW_LINE Print ( n , k ) ; NEW_LINE