← Unit 2 · all exercises
Saved in this browser only

Add up the digits

Return the sum of the digits of a positive number. For 4728 that is 4+7+2+8 = 21.

int digitSum(int n)

Solution.java
Loading editor…
Results

Examples

  • digitSum(4728) → 21
  • digitSum(5) → 5

There are also 4 hidden tests you can't see. Solve the problem, not the examples.