Print first k digits of 1 / n where n is a positive integer | Java 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
import java . io . * ; class GFG { static void print ( int n , int k ) { int rem = 1 ; for ( int i = 0 ; i < k ; i ++ ) { System . out . print ( ( 10 * rem ) / n ) ; rem = ( 10 * rem ) % n ; } } public static void main ( String [ ] args ) { int n = 7 , k = 3 ; print ( n , k ) ; System . out . println ( ) ; n = 21 ; k = 4 ; print ( n , k ) ; } }