Sum of Digits
Given a non-negative integer n, compute and return the sum of all its decimal digits. For example, the digits of 4921 are 4, 9, 2, and 1, which sum to 16.
Given a non-negative integer n, compute and return the sum of all its decimal digits. For example, the digits of 4921 are 4, 9, 2, and 1, which sum to 16.
- `0 <= n <= 10^9` - The input is a single non-negative integer; no leading zeros appear except for the value 0 itself.
Example 1: input n = 4921; output 16. The digits are 4, 9, 2, and 1. Adding them gives 4 + 9 + 2 + 1 = 16.
Example 2: input n = 730; output 10. The digits are 7, 3, and 0. Adding them gives 7 + 3 + 0 = 10.
Example 3: input n = 0; output 0. The number 0 has a single digit whose value is 0, so the sum is 0.
Category: Math. Topic: math. Difficulty: Easy. Patterns: math.