Skip to main content

Practice · 3 of 5

Digit sums with % and /

CHALLENGE
Difficulty: beginner+25 XP

Implement int digitSum(int n): the sum of the digits of non-negative n — e.g. digitSum(1305) is 9. Combine a while loop with % 10 (last digit) and / 10 (drop last digit).

Back to lesson: Practice: Loop rep