Math

Problem shape

Beginner
4
8
15
16
23
42
Core idea

Repeatedly take the last digit and divide the number by ten.

Step 1 / 5
Problem shape
Current lesson

Sum of Digits Lab

Math Beginner

Sum of Digits

Welcome to the Sum of Digits algorithm lab. Repeatedly take the last digit and divide the number by ten. Use it when: Use for number decomposition and digit-processing practice. The time complexity is O(number of digits), and the space complexity is O(1). The pseudocode is: sum = 0 while n > 0 sum += n % 10 n = n / 10 return sum As you practice, track the state that changes and the condition that makes the algorithm stop.

Time

O(number of digits)

Space

O(1)

Pseudocode

sum = 0
while n > 0
  sum += n % 10
  n = n / 10
return sum
Open deeper structure lesson
1.0x