Skip to content
Related Articles

Related Articles

Improve Article
Check if sum of digits of a number exceeds the product of digits of that number
  • Last Updated : 26 May, 2021

Given a positive integer N, the task is to check if the sum of the digits of N is strictly greater than the product of the digits of N or not. If found to be true, then print “Yes”. Otherwise, print “No”.

Examples:

Input: N = 1234
Output: No
Explanation:
The sum of the digits of N(= 1234) is = 1 + 2 + 3 + 4 = 10.
The product of the digits of N(= 1234) is 1*2*3*4 = 24.
As the sum of the digits is smaller than the product of the array. Therefore, print No.

Input: N = 1024
Output: Yes

Approach: Follow the steps below to solve the given problem:



  • Initialize two variables, say sumOfDigit as 0 and prodOfDigit as 1 that stores the sum and the product of the digits of N.
  • Iterate until N is greater than 0 and perform the following steps:
    • Find the last digit of N and store it in a variable, say rem.
    • Increment the value of sumOfDigit by rem.
    • Update the value of prodOfDigit as prodOfDigit*rem.
  • After completing the above steps, if the value of sumOfDigit is greater than prodOfDigit then print “Yes”. Otherwise, print “No”.

Below is the implementation of the above approach:

C++




// C++ program for the above approach
#include <iostream>
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 coode is contributed by code_hunt.

Javascript




<script>
 
// JavaScript 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
function check(n)
{
      
    // Stores the sum and the product of
    // the digits of N
    let sumOfDigit = 0;
    let prodOfDigit = 1;
  
    while (n > 0)
    {
          
        // Stores the last digit if N
        let rem;
        rem = n % 10;
  
        // Increment the value of
        // sumOfDigits
        sumOfDigit += rem;
  
        // Update the prodOfDigit
        prodOfDigit *= rem;
  
        // Divide N by 10
        n = Math.floor(n / 10);
    }
  
    // Prlet the result
    if (sumOfDigit > prodOfDigit)
        document.write("Yes");
    else
        document.write("No");
}
 
// Driver Code
 
    let N = 1234;
      
    check(N);   
 
</script>
Output: 
No

 

Time Complexity: O(log10N)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important mathematical concepts for competitive programming with the Essential Maths for CP Course at a student-friendly price. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.




My Personal Notes arrow_drop_up
Recommended Articles
Page :