For instance, an integer **xyz = 100 x + 10y +z**,

**xyz**% 9 = 99*

**x**+ 9*

**y**+ (

**x**+

**y**+

**z**) % 9 = (

**x**+

**y**+

**z**) % 9. Thus here is my code:

```
public int addDigits(int num) {
if (num == 0) return 0;
while (num > 9) num %= 9;
return num == 0 ? 9 : num;
}
```