Given an integer num, repeatedly add all its digits until the result has only one digit, and return it.
Input: num = 38
Output: 2
Explanation: The process is
38 --> 3 + 8 --> 11
11 --> 1 + 1 --> 2
Since 2 has only one digit, return it.
Input: num = 0
Output: 0
From: LeetCode
Link: 258. Add Digits
1. Digital Root Concept: The digital root of a number is the single-digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration. The digital root can be directly calculated using the formula:
2. Mathematical Insight:
3. Code Explanation:
int addDigits(int num) { if (num == 0) return 0; return (num % 9 == 0) ? 9 : (num % 9); }