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”.
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), as we are traversing the digits which will cost log10N time.
Auxiliary Space: O(1), as we are not using any extra space.