![]() |
VOOZH | about |
Given a number, the task is to find the product of the digits of a number.
Examples:
Input: n = 4513 Output: 60 Input: n = 5249 Output: 360
General Algorithm for product of digits in a given number:
Below is the solution to get the product of the digits:
60
Time Complexity: O(log10N)
Auxiliary Space: O(1)
When this method can be used?: When the number of digits of a number exceeds , we can’t take that number as an integer since the range of long long int doesn’t satisfy the given number. So take input as a string, run a loop from start to the length of the string and increase the sum with that character(in this case it is numeric)
Below is the implementation:
60
Time Complexity: O(N)
Auxiliary Space: O(1)
Method #3: Recursion
10
Time Complexity: O(log10N)
Auxiliary Space: O(1)