Check if sum of digits of a number exceeds the product of digits of that number
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”.
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.
Input: N = 1234
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
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:
Time Complexity: O(log10N)
Auxiliary Space: O(1)