Write a recursive function that returns the product of the digits of its integer input parameter, n. You omay assume that n is non-negative. For example, productDigits(243) should return 24, since 2 x 4 x 3

Respuesta :

Answer:

The solution code is written in Python:

  1. def productDigits(n):
  2.    if(n // 10 == 0  ):
  3.        return n
  4.    else:
  5.        return (n%10) * productDigits(n//10)

Explanation:

Firstly we define a function named it as productDigits which takes one input parameter, n. (Line 1)

Next, we create if-else statements by setting the condition if the input n divided by 10 is equal to 0 (this means the n is only 1 digit), the program shall return the n (Line 3-4).

Otherwise, the program will return the remainder of the n divided by 10 multiplied with the output of the recursive function calling with input n//10 (Line 5-6).

Please note the usage of // is to discard the decimal point of the division result.