---
# System prepended metadata

title: 'Sum of Digits / Digital Root [6 kyu]'
tags: [C++, CodeWars]

---

# Sum of Digits / Digital Root [6 kyu]

[Sum of Digits / Digital Root](https://www.codewars.com/kata/541c8630095125aba6000c00)
6 kyu

## Solution
```cpp=
/**
*** Author: R-CO
*** E-Mail: daniel1820kobe@gmail.com
*** Date: 2020-07-28
**/
#include <cctype>
#include <cstdlib>
#include <iostream>
using std::cout;
using std::endl;

struct TestCaseStruct {
  int input;
  int expected_output;
};

class DigitalRoot {
 public:
  int digital_root(int n) {
    int output = n;

    while (output >= 10) {
      auto temp = output;
      output = 0;
      while (temp != 0) {
        output += temp % 10;
        temp /= 10;
      }
    }

    return output;
  }
};

int main(int argc, char *argv[]) {
  /*
  It(Digital_root)
    {
        Assert::That(digital_root(16) , Equals(7));
        Assert::That(digital_root(195) , Equals(6));
        Assert::That(digital_root(992) , Equals(2));
        Assert::That(digital_root(167346) , Equals(9));
        Assert::That(digital_root(0) , Equals(0));
    }
  */
  TestCaseStruct test_cases[] = {
      {16, 7}, {195, 6}, {992, 2}, {167346, 9}, {0, 0}};

  DigitalRoot solution;
  for (const auto& test_case : test_cases) {
    if (solution.digital_root(test_case.input) == test_case.expected_output) {
      cout << "test case \"" << test_case.input << "\" is pass." << std::endl;
    } else {
      cout << "test case \"" << test_case.input << "\" is fail." << std::endl;
    }
  }

  return EXIT_SUCCESS;
}

```

## Result

PASS


###### tags: `CodeWars` `C++`
