Dados dos números enteros L y R , la tarea es encontrar el producto acumulativo de dígitos (es decir, el producto del producto de dígitos) de todos los números naturales en el rango L a R.
Ejemplos :
Entrada: L = 2, R = 5
Salida: 14
Explicación:
2 * 3 * 4 * 5 = 120
Entrada: L = 11, R = 15
Salida: 120
Explicación:
(1*1) * (1*2) * ( 1*3) * (1*4) * (1*5) = 1 * 2 * 3 * 4 * 5 = 120
Planteamiento:
Para resolver el problema mencionado anteriormente tenemos que observar que si:
- Si la diferencia entre L y R es mayor que 9 , entonces el producto es 0 porque aparece un dígito 0 en cada número después de intervalos de 9.
- De lo contrario, podemos encontrar el producto en un ciclo de L a R, el ciclo se ejecutará un máximo de 9 veces.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ program to print the product // of all numbers in range L and R #include <bits/stdc++.h> using namespace std; // Function to get product of digits int getProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function to find the product of digits // of all natural numbers in range L to R int productinRange(int l, int r) { if (r - l > 9) return 0; else { int p = 1; // Iterate between L to R for (int i = l; i <= r; i++) p *= getProduct(i); return p; } } // Driver Code int main() { int l = 11, r = 15; cout << productinRange(l, r) << endl; l = 1, r = 15; cout << productinRange(l, r); return 0; }
Java
// Java program to print the product // of all numbers in range L and R class GFG{ // Function to get product of digits static int getProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function to find the product of digits // of all natural numbers in range L to R static int productinRange(int l, int r) { if (r - l > 9) return 0; else { int p = 1; // Iterate between L to R for (int i = l; i <= r; i++) p *= getProduct(i); return p; } } // Driver Code public static void main(String[] args) { int l = 11, r = 15; System.out.print(productinRange(l, r) + "\n"); l = 1; r = 15; System.out.print(productinRange(l, r)); } } // This code is contributed by Rohit_ranjan
Python3
# Python3 program to print the product # of all numbers in range L and R # Function to get product of digits def getProduct(n): product = 1 while (n != 0): product = product * (n % 10) n = int(n / 10) return product # Function to find the product of digits # of all natural numbers in range L to R def productinRange(l, r): if (r - l > 9): return 0 else: p = 1 # Iterate between L to R for i in range(l, r + 1): p = p * getProduct(i) return p # Driver Code l = 11 r = 15 print (productinRange(l, r), end='\n') l = 1 r = 15 print (productinRange(l, r)) # This code is contributed by PratikBasu
C#
// C# program to print the product // of all numbers in range L and R using System; class GFG{ // Function to get product of digits static int getProduct(int n) { int product = 1; while (n != 0) { product = product * (n % 10); n = n / 10; } return product; } // Function to find the product of digits // of all natural numbers in range L to R static int productinRange(int l, int r) { if (r - l > 9) return 0; else { int p = 1; // Iterate between L to R for(int i = l; i <= r; i++) p *= getProduct(i); return p; } } // Driver Code public static void Main(String[] args) { int l = 11, r = 15; Console.Write(productinRange(l, r) + "\n"); l = 1; r = 15; Console.Write(productinRange(l, r)); } } // This code is contributed by amal kumar choubey
Javascript
<script> // Javascript program to print the product // of all numbers in range L and R // Function to get product of digits function getProduct(n) { var product = 1; while (n != 0) { product = product * (n % 10); n = parseInt(n / 10); } return product; } // Function to find the product of digits // of all natural numbers in range L to R function productinRange(l, r) { if (r - l > 9) return 0; else { var p = 1; // Iterate between L to R for (var i = l; i <= r; i++) p *= getProduct(i); return p; } } // Driver Code var l = 11, r = 15; document.write( productinRange(l, r)+ "<br>"); l = 1, r = 15; document.write( productinRange(l, r)); // This code is contributed by rutvik_56. </script>
Producción:
120 0