Ask Question

The code below returns the number of zeros at the end of n! [factorial n]int zeros (int n) {int res = 0; while (n!=0) {res + = n/5; n / = 5; }return res; }Rewrite this method recursively:6. 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 = 24. int productDigits (int n)

+5
Answers (1)
  1. 27 August, 03:19
    0
    Answer to 1:

    The recursive method is as follows:

    int zeros (int n) {

    if (n = = 0) return 0;

    return n/5 + zeros (n/5); }

    Explanation:

    In the above code, the zeros () function is a recursive function as it is calling itself. When this function executes, it calls itself. Lets see how this method works.

    Suppose n = 10

    first it checks if n is equals to 0. As n is 10 so the program flow moves to the next statement: return n/5 + zeros (n/5); which calls zeros () function again. This recursive function will keep calling itself until the base condition evaluates to true.

    Here the base case is when the value of n becomes 0 and the recursive case is the n/5 + zeros (n/5) which keeps calling itself (zeros function) recursively.

    Now lets see what return n/5 + zeros (n/5); statement does.

    At first recursive call

    10/5 + zeros (10/5)

    2 + zeros (2)

    Now here the zeros function called itself again so:

    zeros (2) the if condition checks if 2 = = 0 which is false as 2>0 So return n/5 + zeros (n/5); statement executes again

    2/5 + zeros (2/5)

    This basically takes this form:

    2 + [2/5 + zeros (2/5) ]

    2 + [0 + zeros (0) ]

    The answer to 2/5 is 0.4 but here we are using int for n so the rounded number is 0.

    Now zeros (0) will again call zeros () function. Now n = 0 So if condition checks if n==0 which is true. The base condition evaluates to true means the recursion will stop now.

    Now what is returned in output finally?

    2 + [0] = 2

    So this recursive function returns 2.

    If you want to see the results of this function in output screen you can write this main () function which takes a number in input and calls zeros () function.

    int main ()

    {int num;

    cin>>num;

    cout<
    Answer to 2

    Here is the recursive function in C++:

    int product_of_digit (int number)

    {

    if (number < 10)

    return number;

    return product_of_digit (number / 10) * (number % 10);

    }

    Explanation:

    Here the product_of_digit () is a recursive function.

    Base case is when the input number is less than 10. Otherwise the recursive function keeps calling itself and the recursive case is the following statement:

    return product_of_digit (number / 10) * (number % 10);

    Now lets see how this function works for the input number = 243

    First the If condition checks if the number<10 which is false as number = 243

    So the recursive statement is executed which keeps calling itself until it reaches base condition.

    return product_of_digit (number / 10) * (number % 10);

    product_of_digit (243 / 10) * (243 % 10)

    Here dividing 243 by 10 gives 24 as we are using int data type for the value of number so this will ignore the fraction part and gives a rounded result.

    Taking mod of 243 % 10 as we know gives the remainder so 243%10 = 3

    product_of_digit (24) * 3

    Now product_of_digit () function calls itself again and this time its argument (parameter) is 24.

    So first the If condition which is the base case is checked. This evaluates to false as 24 is not less than 10. So program control skips the statement inside if condition and moves to the next statement which is the recursion statement.

    return product_of_digit (number / 10) * (number % 10);

    [ product_of_digit (24 / 10) * 24 % 10 ] * 3

    Now the above statement is for the number 24 and the 3 at the end is of the previous recursion.

    [product_of_digit (2) * 4 ] * 3

    Now product_of_digit () function is called again with the parameter this time is value of number = 2. Now the base case evaluates to true as the value of number is 2 which is less than 10. So the recursion stops. The statement after the if condition is now executed which is: return number; The current value of number = 2 So this statement returns 2.

    So what is going to be returned now?

    [product_of_digit (2) * 4 ] * 3

    The product_of_digit (2) returns 2 So

    2 * 4 * 3 = 24

    So the recursive function product_of_digits () returns 24.

    In order to see the results we can write a main () function as follows:

    int main ()

    { int num;

    cout<<"Enter a number";

    cin>>num;

    int result = product_of_digit (num);

    cout << "Product of digits "<< num<<" is "<
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “The code below returns the number of zeros at the end of n! [factorial n]int zeros (int n) {int res = 0; while (n!=0) {res + = n/5; n / = ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers