Cumulative product of digits of all numbers in the given range - GeeksforGeeks

20 Apr, 2021
Given two integers L and R, the task is to find the cumulative product of digits (i.e. product of the product of digits) of all Natural numbers in the range L to R. Examples:  
Input: L = 2, R = 5 Output: 14 Explanation: 2 * 3 * 4 * 5 = 120Input: L = 11, R = 15 Output: 120 Explanation: (1*1) * (1*2) * (1*3) * (1*4) * (1*5) = 1 * 2 * 3 * 4 * 5 = 120  
 
Approach:To solve the problem mentioned above we have to observe that if:  
If the difference between L and R is greater than 9 then the product is 0 because there appears a digit 0 in every number after intervals of 9.
Otherwise, We can find the product in a loop from L to R, the loop will run a maximum of 9 times.
Below is the implementation of the above approach:  
C++
Java
Python3
C#
Javascript
// 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 digitsint 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 Rint 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 Codeint main(){    int l = 11, r = 15;    cout << productinRange(l, r)         << endl;     l = 1, r = 15;    cout << productinRange(l, r);     return 0;}
// Java program to print the product// of all numbers in range L and Rclass GFG{ // Function to get product of digitsstatic 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 Rstatic 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 Codepublic 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 program to print the product# of all numbers in range L and R # Function to get product of digitsdef 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 Rdef 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 Codel = 11r = 15print (productinRange(l, r), end='\n') l = 1r = 15print (productinRange(l, r)) # This code is contributed by PratikBasu
// C# program to print the product// of all numbers in range L and Rusing System; class GFG{ // Function to get product of digitsstatic 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 Rstatic 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 Codepublic 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
<script> // Javascript program to print the product// of all numbers in range L and R // Function to get product of digitsfunction 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 Rfunction 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 Codevar 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>
120
0
 
Rohit_ranjan
PratikBasu
Amal Kumar Choubey
rutvik_56
array-range-queries
Natural Numbers
number-digits
Arrays
Mathematical
Pattern Searching
Arrays
Mathematical
Pattern Searching
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Multidimensional Arrays in Java
Introduction to Arrays
Python | Using 2D arrays/lists the right way
Linked List vs Array
Program for Fibonacci numbers
Write a program to print all permutations of a given string
C++ Data Types
Set in C++ Standard Template Library (STL)
Coin Change | DP-7