📌  相关文章
📜  给定范围内所有数字的数字的累积乘积

📅  最后修改于: 2021-09-06 11:05:54             🧑  作者: Mango

给定两个整数 L 和 R ,任务是找到 L 到 R 范围内所有自然数的数字的累积乘积(即数字的乘积)。
例子

方法:
为了解决上面提到的问题,我们必须观察,如果:

  • 如果L 和 R之间的差值大于 9,则乘积为 0,因为在间隔 9 之后的每个数字中都会出现一个数字 0。
  • 否则,我们可以在从 L 到 R 的循环中找到乘积,循环最多运行 9 次。

下面是上述方法的实现:

C++
// C++ program to print the product
// of all numbers in range L and R
 
#include 
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


输出:
120
0