Loops · Intermediate · 55 XP · about 5 min
Write a function called sum_of_digits(n) that returns the sum of all digits in a non-negative integer.
sum_of_digits(123) # Returns 6 (1 + 2 + 3) sum_of_digits(9999) # Returns 36 (9 + 9 + 9 + 9) sum_of_digits(0) # Returns 0 sum_of_digits(7) # Returns 7
Ready?
Accept the challenge to start writing. A small timer counts the time this page is in view.
Output appears here.