Dados tres enteros P , Q y N donde P < Q , la tarea es calcular el valor de la fracción de P / Q y encontrar el dígito N después del decimal.
Ejemplo
Entrada: P = 1, Q = 2, N = 1
Salida: 5
(1/2) = 0,5 y 5 es el primer dígito después del decimal.
Entrada: P = 5, Q = 6, N = 5
Salida: 3
(5 / 6) = 0,833333333…
Enfoque: inicialice una variable entera res que almacene el enésimo dígito resultante. Ahora, mientras N > 0 haga lo siguiente:
- Disminuya N en 1 .
- Para calcular el dígito, actualice el valor P = P * 10 .
- Calcule res = P / Q y actualice P = P % Q .
Finalmente, imprima el res.
A continuación se muestra la implementación del enfoque anterior:
C++
// 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; }
Java
// 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
Python3
# 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
C#
// 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
Javascript
<script> // JavaScript implementation of the approach // Function to print the Nth digit // in the fraction (p / q) function findNthDigit(p, q, N) { // To store the resultant digit var 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 = parseInt(p / q); p %= q; } return res; } // Driver code var p = 1, q = 2, N = 1; document.write(findNthDigit(p, q, N)); // This code is contributed by rdtank. </script>
Producción
5
Complejidad de tiempo: O(N)
Espacio Auxiliar : O(1)