Given three integers P, Q and N where P < Q, the task is to compute the fraction value of P / Q and find the Nth digit after the decimal.
Input: P = 1, Q = 2, N = 1
(1 / 2) = 0.5 and 5 is the first digit after the decimal.
Input: P = 5, Q = 6, N = 5
(5 / 6) = 0.833333333…
Approach: Initialize an integer variable res that stores the resultant Nth digit. Now, while N > 0 do the following:
- Decrement N by 1.
- To compute the digit, update the value P = P * 10.
- Compute res = P / Q and update P = P % Q.
Finally, print the res.
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Largest proper fraction with sum of numerator and denominator equal to a given number
- First occurrence of a digit in a given fraction
- Count of N-digit numbers having digit XOR as single digit
- Represent the fraction of two numbers in the string format
- Sum of all proper divisors of natural numbers in an array
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Find N fractions that sum upto a given fraction N/D
- Find Recurring Sequence in a Fraction
- Sum of all proper divisors of a natural number
- Sum of all proper divisors from 1 to N
- Print all proper fractions with denominators less than equal to N
- Product of proper divisors of a number for Q queries
- Find nth number that contains the digit k or divisible by k.
- Find the Nth digit from right in base B of the given number in Decimal base
- Greedy Algorithm for Egyptian Fraction
- Convert decimal fraction to binary number
- Fraction module in Python
- as_integer_ratio() in Python for reduced fraction of a given rational
- Maximum rational number (or fraction) from an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.