Question

Write c++ recursive function 3. A function that takes an integer, and returns the product of...

Write c++ recursive function

3. A function that takes an integer, and returns the product of the digits, so the input 1989 would return 1∗9∗8∗9 = 648

4. A function that does the above repeatedly, taking a number and computing the product

of it’s digits until you get a single digit number: so on input 1989 it would go 1989→648→192→18→8 and return 8. (Note, you can use the previous functions if you want.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>

using namespace std;

int product(int n) {
    if (n < 10) {
        return n;
    } else {
        return (n % 10) * product(n / 10);
    }
}

int product_digit(int n) {
    if (n < 10) {
        return n;
    } else {
        return product_digit(product(n));
    }
}

int main() {
    cout << product(1989) << endl;
    cout << product_digit(1989) << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write c++ recursive function 3. A function that takes an integer, and returns the product of...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT