📜  检查一个数字的数字总和是否超过该数字的数字乘积

📅  最后修改于: 2022-05-13 01:56:07.104000             🧑  作者: Mango

检查一个数字的数字总和是否超过该数字的数字乘积

给定一个正整数N ,任务是检查N的数字之和是否严格大于N的数字的乘积。如果发现是真的,则打印“是” 。否则,打印“否”

例子:

方法:按照以下步骤解决给定问题:

  • 初始化两个变量,比如sumOfDigit0prodOfDigit1存储N的数字的总和和乘积。
  • 迭代直到N大于0并执行以下步骤:
    • 找到N的最后一位并将其存储在一个变量中,比如rem
    • sumOfDigit的值增加rem
    • 将 prodOfDigit 的值更新为prodOfDigit *rem
  • 完成上述步骤后,如果sumOfDigit的值大于prodOfDigit则打印“Yes” 。否则,打印“否”

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to check if the sum of the
// digits of N is strictly greater than
// the product of the digits of N or not
void check(int n)
{
    // Stores the sum and the product of
    // the digits of N
    int sumOfDigit = 0;
    int prodOfDigit = 1;
 
    while (n > 0) {
 
        // Stores the last digit if N
        int rem;
        rem = n % 10;
 
        // Increment the value of
        // sumOfDigits
        sumOfDigit += rem;
 
        // Update the prodOfDigit
        prodOfDigit *= rem;
 
        // Divide N by 10
        n /= 10;
    }
 
    // Print the result
    if (sumOfDigit > prodOfDigit)
        cout << "Yes";
    else
        cout << "No";
}
 
// Driver Code
int main()
{
    int N = 1234;
    check(N);
 
    return 0;
}


Java
// Java program for the above approach
class GFG{
 
// Function to check if the sum of the
// digits of N is strictly greater than
// the product of the digits of N or not
static void check(int n)
{
     
    // Stores the sum and the product of
    // the digits of N
    int sumOfDigit = 0;
    int prodOfDigit = 1;
 
    while (n > 0)
    {
         
        // Stores the last digit if N
        int rem;
        rem = n % 10;
 
        // Increment the value of
        // sumOfDigits
        sumOfDigit += rem;
 
        // Update the prodOfDigit
        prodOfDigit *= rem;
 
        // Divide N by 10
        n /= 10;
    }
 
    // Print the result
    if (sumOfDigit > prodOfDigit)
        System.out.println("Yes");
    else
        System.out.println("No");
}
 
// Driver code
public static void main(String[] args)
{
    int N = 1234;
     
    check(N);
}
}
 
// This code is contributed by abhinavjain194


Python3
# Python3 program for the above approach
 
# Function to check if the sum of the
# digits of N is strictly greater than
# the product of the digits of N or not
def check(n):
     
    # Stores the sum and the product of
    # the digits of N
    sumOfDigit = 0
    prodOfDigit = 1
 
    while n > 0:
         
        # Stores the last digit if N
        rem = n % 10
 
        # Increment the value of
        # sumOfDigits
        sumOfDigit += rem
 
        # Update the prodOfDigit
        prodOfDigit *= rem
 
        # Divide N by 10
        n = n // 10
 
    # Print the result
    if sumOfDigit > prodOfDigit:
        print("Yes")
    else:
        print("No")
 
# Driver Code
N = 1234
     
check(N)
 
# This code is contributed by jana_sayantan


C#
// C# program for the above approach
using System;
  
class GFG{
  
// Function to check if the sum of the
// digits of N is strictly greater than
// the product of the digits of N or not
static void check(int n)
{
      
    // Stores the sum and the product of
    // the digits of N
    int sumOfDigit = 0;
    int prodOfDigit = 1;
  
    while (n > 0)
    {
          
        // Stores the last digit if N
        int rem;
        rem = n % 10;
  
        // Increment the value of
        // sumOfDigits
        sumOfDigit += rem;
  
        // Update the prodOfDigit
        prodOfDigit *= rem;
  
        // Divide N by 10
        n /= 10;
    }
  
    // Print the result
    if (sumOfDigit > prodOfDigit)
        Console.WriteLine("Yes");
    else
        Console.WriteLine("No");
}
 
  
// Driver Code
public static void Main()
{
    int N = 1234;
      
    check(N);
      
}
}
 
// This code is contributed by code_hunt.


Javascript


输出:
No

时间复杂度: O(log 10 N)
辅助空间: O(1)