Dynamic Programming21 sections · 916 units
Open in Course

Sum of Digits - Base Case

Single digit

The base case is when n<10n < 10, meaning nn is a single digit. In this case, the sum is just nn itself. For example, if n=7n = 7, the sum of digits is 77. You do not need recursion here.

This stopping condition keeps you do not try to break down a single digit further. Once you have reduced the number to one digit through repeated division by 1010, you are done. No more digits to peel off. That's your signal to stop.