Question

2. Consider the following recursive function (Chapter 17, #9, modified) void recFun (int x) { if (x > 10) { recFun (x / 10);
0 0
Add a comment Improve this question Transcribed image text
Answer #1

The recFun() prints the digits of the number that is passed. So whatever number is passed, it prints the same number .

Answer a) The call recFun(268) prints 268

Answer b) To print the digits one on each line, we can modify the function as follows-
int recFun(int x)
{
   int sum = 0;
   if(x > 0)
   {
       int sum = recFun(x/10) + x%10;
   }
   else
       sum = x;
   cout << sum << endl;
}


Answer c) To print the sum of digits, the code can be modified as follows-
int recFun(int x, int sum = 0)
{
   if(x <= 9)
       cout << x+sum << endl;
   else
       recFun(x / 10, sum+x%10);
}

Add a comment
Know the answer?
Add Answer to:
2. Consider the following recursive function (Chapter 17, #9, modified) void recFun (int x) { if...
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