Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- int digital_root(int n) {
- int result;
- int tmp = n;
- do {
- result = sum_digits(tmp);
- tmp = result;
- } while (result >= 10);
- return result;
- }
- // returns the sum of all digits in n
- int sum_digits(int n) {
- if (n / 10 == 0)
- {
- return n;
- }
- else
- {
- return n%10 + digital_root(n/10);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement