Math Fundamentals18 sections · 814 units
Open in Course

Problem - Add Digits

LeetCode 258

Given a non-negative integer nn, repeatedly add all its digits until the result has only one digit. Return that digit.

For example, if n=38n = 38, add 3+8=113 + 8 = 11. Then add 1+1=21 + 1 = 2. Return 22.

There's a simple loop solution, but also a mathematical shortcut using modulo. Try the loop first.