1. The digital root of a number is obtained by summing up the digits repeatedly until only a single digit remains. For example, the digital root of 7854 is obtained by computing 7+8+5+4 which is 24. Next the digits 2 and 4 are summed to yield 6. Since it is a single digit, this forms the digital root of the given number. Write a recursive function root(int n) that returns the digital root of the argument without making use of any loop constructs ( like for, while etc.) in your function. - 1 root(n) if ( n/10) ==0) return ( n % 10 ); else return root( n % 10 + root (n /10 ) );

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter15: Recursion
Section: Chapter Questions
Problem 8SA
icon
Related questions
Question
Please trace the recursive calls of this function
1.
The digital root of a number is obtained by summing up the digits
repeatedly until only a single digit remains. For example, the digital root of 7854 is
obtained by computing 7+8+5+4 which is 24. Next the digits 2 and 4 are summed to yield
6. Since it is a single digit, this forms the digital root of the given number. Write a
recursive function root(int n) that returns the digital root of the argument without making
use of any loop constructs ( like for, while etc.) in your function.
root(n)
if ( n/10) ==0 )
return ( n % 10 );
else
return root( n % 10 + root ( n /10 ) );
Transcribed Image Text:1. The digital root of a number is obtained by summing up the digits repeatedly until only a single digit remains. For example, the digital root of 7854 is obtained by computing 7+8+5+4 which is 24. Next the digits 2 and 4 are summed to yield 6. Since it is a single digit, this forms the digital root of the given number. Write a recursive function root(int n) that returns the digital root of the argument without making use of any loop constructs ( like for, while etc.) in your function. root(n) if ( n/10) ==0 ) return ( n % 10 ); else return root( n % 10 + root ( n /10 ) );
Expert Solution
steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Computational Systems
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning