# Cumulative product of digits of all numbers in the given range

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 = 120

Input: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++

`// 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; ` `} ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

## 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 ` |

*chevron_right*

*filter_none*

**Output:**

120 0

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.

## Recommended Posts:

- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Find the number in a range having maximum product of the digits
- Sort the numbers according to their product of digits
- Number of digits in the product of two numbers
- Find last k digits in product of an array numbers
- Find the maximum sum of digits of the product of two numbers
- Find maximum product of digits among numbers less than or equal to N
- Count of numbers with all digits same in a given range
- Count of numbers from range [L, R] that end with any of the given digits
- Count of numbers from range [L, R] whose sum of digits is Y
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of Numbers in Range where the number does not contain more than K non zero digits
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Total numbers with no repeated digits in a range
- Sum of product of proper divisors of all Numbers lying in range [L, R]
- Find all the possible numbers in a range that can be evenly divided by its digits
- Numbers with a Fibonacci difference between Sum of digits at even and odd positions in a given range
- Count Numbers in Range with difference between Sum of digits at even and odd positions as Prime
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number

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.