Consider the process in Which we take a number and then suma its digits. Then we
ID: 3820220 • Letter: C
Question
Consider the process in Which we take a number and then suma its digits. Then we repeat this process until the remaining number has only one digit. This digit is called the digital root of the number. For example, starting from the mumber 9876, we get 30 and then 3. The digital sum of 9876 is 3 The number of times we have to repeat the process before finding the digital root is called the additive persistence of the number. The additive persistence of9876 is 2 1) Write a function that uses the method described above to find the digital root and the additive persistence of a given number, n. You function should returm the digital root as its result and also set a second parameter to the value of the additive persistence, Your definition should have the prototype: int digital root (int, int 2) Write a program that prints a table of digital roots of the numbers between 1 and 1000 (Aside: After looking at this table we can see that the digital root can be computed without performing any iterations using the fomula below. You should not use this fomula in part (1) but you should solve the problem by actually summing the digits. In any case, this does not give the additive persistence. Just for interest, the fomula for digital roots is: m (mod 9), n not divisible by 9 root(n) 9,n divisible by 9Explanation / Answer
#include <iostream>
using namespace std;
int sum_digit(int n)
{
int sum = 0;
while(n != 0)
{
sum += n %10;
n = n/10;
}
return sum;
}
int digital_root(int num, int *additive)
{
*additive = 0;
while(!(num <= 9 && num >= 0))
{
num = sum_digit(num);
*additive = *additive + 1;
}
return num;
}
int main()
{
for(int num = 1; num <= 1000; num++)
{
int additive = 0;
int sum = digital_root(num, &additive);
cout << "Num: " << num << " Digital root: " << sum << " Additive persistence: " << additive << endl;
}
return 0;
}
Please post one question at a time.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.