Print first k digits of 1/n where n is a positive integer
Given a positive integer n, print first k digits after point in value of 1/n. Your program should avoid overflow and floating point arithmetic.
Examples :
Input: n = 3, k = 3 Output: 333 Input: n = 50, k = 4 Output: 0200
We strongly recommend to minimize the browser and try this yourself first.
Let us consider an example n = 7, k = 3. The first digit of 1/7 is ‘1’, it can be obtained by doing integer value of 10/7. Remainder of 10/7 is 3. Next digit is 4 which can be obtained by taking integer value of 30/7. Remainder of 30/7 is 2. Next digits is 2 which can be obtained by taking integer value of 20/7
C++
#include <iostream> using namespace std; // Function to print first k digits after dot in value // of 1/n. n is assumed to be a positive integer. void print(int n, int k) { int rem = 1; // Initialize remainder // Run a loop k times to print k digits for (int i = 0; i < k; i++) { // The next digit can always be obtained as // doing (10*rem)/10 cout << (10 * rem) / n; // Update remainder rem = (10*rem) % n; } } // Driver program to test above function int main() { int n = 7, k = 3; print(n, k); cout << endl; n = 21, k = 4; print(n, k); return 0; } |
Java
// Java code to Print first k // digits of 1/n where n is a // positive integer import java.io.*; class GFG { // Function to print first // k digits after dot in value // of 1/n. n is assumed to be // a positive integer. static void print(int n, int k) { // Initialize remainder int rem = 1; // Run a loop k times to print k digits for (int i = 0; i < k; i++) { // The next digit can always be // obtained as doing (10*rem)/10 System.out.print( (10 * rem) / n); // Update remainder rem = (10 * rem) % n; } } // Driver program public static void main(String []args) { int n = 7, k = 3; print(n, k); System.out.println(); n = 21; k = 4; print(n, k); } } // This article is contributed by vt_m |
Python3
# Python code to Print first k # digits of 1/n where n is a # positive integer import math # Function to print first k digits # after dot in value of 1/n. n is # assumed to be a positive integer. def Print(n, k): rem = 1 # Initialize remainder # Run a loop k times to print # k digits for i in range(0, k): # The next digit can always # be obtained as doing # (10*rem)/10 print(math.floor(((10 * rem) / n)), end="") # Update remainder rem = (10*rem) % n # Driver program to test # above function n = 7k = 3Print(n, k); print(" ") n = 21k = 4Print(n, k); # This code is contributed by Sam007. |
C#
// C# code to Print first k digits of // 1/n where n is a positive integer using System; class GFG { // Function to print first // k digits after dot in value // of 1/n. n is assumed to be // a positive integer. static void print(int n, int k) { // Initialize remainder int rem = 1; // Run a loop k times to // print k digits for (int i = 0; i < k; i++) { // The next digit can always be // obtained as doing (10*rem)/10 Console.Write( (10 * rem) / n); // Update remainder rem = (10 * rem) % n; } } // Driver program public static void Main() { int n = 7, k = 3; print(n, k); Console.WriteLine(); n = 21; k = 4; print(n, k); } } // This code is contributed by Sam007. |
PHP
<?php // Function to print first k digits // after dot in value of 1/n. n is // assumed to be a positive integer. function println($n, $k) { // Initialize remainder $rem = 1; // Run a loop k times // to print k digits for ($i = 0; $i < $k; $i++) { // The next digit can always // be obtained as doing // (10 * rem) / 10 echo floor((10 * $rem) / $n); // Update remainder $rem = (10 * $rem) % $n; } } // Driver Code $n = 7; $k = 3; println($n, $k); echo "\n"; $n = 21; $k = 4; println($n, $k); // This code is contributed by aj_36 ?> |
Output :
142 0476
Reference:
Algorithms And Programming: Problems And Solutions by Alexander Shen
This article is contributed by Sachin. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Minimum positive integer value possible of X for given A and B in X = P*A + Q*B
- Maximum positive integer divisible by C and is in the range [A, B]
- Minimum positive integer divisible by C and is not in range [A, B]
- Minimum positive integer to divide a number such that the result is an odd
- Minimum Possible sum of digits in a positive multiple of N
- Find the smallest positive number which can not be represented by given digits
- Count positive integers with 0 as a digit and maximum 'd' digits
- Count even and odd digits in an Integer
- Program to count digits in an integer (4 Different Methods)
- Reverse digits of an integer with overflow handled
- Print numbers with digits 0 and 1 only such that their sum is N
- Print all possible words from phone digits
- Print all numbers less than N with at-most 2 unique digits
- Print last k digits of a^b (a raised to power b)
- Print all n-digit numbers whose sum of digits equals to given sum



