|Sum of Digits
All Challenges

Sum of Digits

EasyMath

Given a non-negative integer n, return the sum of its digits.


For example, n = 493 → 4 + 9 + 3 = 16.


This shows up constantly in checksum algorithms, validation systems (like credit card numbers), and number theory problems.

Examples

Input: sum_of_digits(493)
Output: 16
4 + 9 + 3 = 16
Input: sum_of_digits(0)
Output: 0
Single digit 0
Input: sum_of_digits(999)
Output: 27
9 + 9 + 9 = 27
Input: sum_of_digits(1000)
Output: 1
1 + 0 + 0 + 0 = 1

Constraints

  • 0 <= n <= 10^9
pythoneditor
Loading Python runtime…