Advertisement
Guest User

Untitled

a guest
May 25th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.33 KB | None | 0 0
  1. int digital_root(int n) {
  2.   int result;
  3.   int tmp = n;
  4.   do {
  5.     result = sum_digits(tmp);
  6.     tmp = result;
  7.   } while (result >= 10);
  8.  
  9.   return result;
  10. }
  11.  
  12. // returns the sum of all digits in n
  13. int sum_digits(int n) {
  14.   if (n / 10 == 0)
  15.   {
  16.     return n;
  17.   }
  18.   else
  19.   {
  20.     return n%10 + digital_root(n/10);
  21.   }
  22. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement