Find the Nth digit in the proper fraction of two numbers

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.

Example

Input: P = 1, Q = 2, N = 1
Output: 5
(1 / 2) = 0.5 and 5 is the first digit after the decimal.

Input: P = 5, Q = 6, N = 5
Output: 3
(5 / 6) = 0.833333333…

Approach: Initialize an integer variable res that stores the resultant Nth digit. Now, while N > 0 do the following:



  1. Decrement N by 1.
  2. To compute the digit, update the value P = P * 10.
  3. Compute res = P / Q and update P = P % Q.

Finally, print the res.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the Nth digit
// in the fraction (p / q)
int findNthDigit(int p, int q, int N)
{
    // To store the resultant digit
    int res;
  
    // While N > 0 compute the Nth digit
    // by dividing p and q and store the
    // result into variable res
    // and go to next digit
    while (N > 0) {
        N--;
        p *= 10;
        res = p / q;
        p %= q;
    }
    return res;
}
  
// Driver code
int main()
{
    int p = 1, q = 2, N = 1;
  
    cout << findNthDigit(p, q, N);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach
class GFG
{
  
    // Function to print the Nth digit 
    // in the fraction (p / q) 
    static int findNthDigit(int p, 
                            int q, int N) 
    
        // To store the resultant digit 
        int res = 0
      
        // While N > 0 compute the Nth digit 
        // by dividing p and q and store the 
        // result into variable res 
        // and go to next digit 
        while (N > 0
        
            N--; 
            p *= 10
            res = p / q; 
            p %= q; 
        
        return res; 
    
      
    // Driver code 
    public static void main(String args[]) 
    
        int p = 1, q = 2, N = 1
      
        System.out.println(findNthDigit(p, q, N)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 implementation of the approach 
  
# Function to print the Nth digit 
# in the fraction (p / q) 
def findNthDigit(p, q, N) : 
  
    # While N > 0 compute the Nth digit 
    # by dividing p and q and store the 
    # result into variable res 
    # and go to next digit 
    while (N > 0) :
        N -= 1
        p *= 10
        res = p // q; 
        p %= q; 
  
    return res; 
  
# Driver code 
if __name__ == "__main__" :
      
    p = 1; q = 2; N = 1;
    print(findNthDigit(p, q, N)); 
  
# This code is contributed by kanugargng

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using System;
  
class GFG
{
  
    // Function to print the Nth digit 
    // in the fraction (p / q) 
    static int findNthDigit(int p, int q, int N) 
    
        // To store the resultant digit 
        int res = 0; 
      
        // While N > 0 compute the Nth digit 
        // by dividing p and q and store the 
        // result into variable res 
        // and go to next digit 
        while (N > 0) 
        
            N--; 
            p *= 10; 
            res = p / q; 
            p %= q; 
        
        return res; 
    
      
    // Driver code 
    public static void Main() 
    
        int p = 1, q = 2, N = 1; 
      
        Console.WriteLine(findNthDigit(p, q, N)); 
    
}
  
// This code is contributed by AnkitRai01

chevron_right


Output:

5

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.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.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.



Improved By : kanugargng, AnkitRai01

Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.