Sum of Digits

Easy~10 min

Given an integer n, return the sum of all digits in the absolute value of n.

For example, sumOfDigits(-123) returns 6 because |-123| = 123 and 1 + 2 + 3 = 6.

Examples

Example 1
Input: n = 123
Output: 6
Explanation: 1 + 2 + 3 = 6.
Example 2
Input: n = -456
Output: 15
Explanation: |-456| = 456, and 4 + 5 + 6 = 15.
Example 3
Input: n = 0
Output: 0
Explanation: The only digit is 0, so the sum is 0.

Constraints

  • -10^9 <= n <= 10^9
  • n is an integer.
Code
Ctrl+EnterRun|Ctrl+⇧+EnterSubmit
Output

Run your code to see results

Use Cmd+Enter to run