Skip to content
Exercise · Loops · lesson 6 of 7

Exercise: digit sum

Write a function digit_sum(n) that returns the sum of the decimal digits of the integer n.

  • digit_sum(123) → 6
  • digit_sum(9045) → 18
  • digit_sum(0) → 0

Constraints: n is from 0 to 1000000000; the number 0 has one digit, and its digit sum is 0.

Do not add extra imports unless the task says so explicitly.

digit_sum.py — python
python · 3 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way