
Nitin P. answered 06/05/20
Computer Science major at UC Berkeley
Use modulo by 10 to get the last digit of a number, add it to a sum variable, then integer division by 10 to chop that number off the end. For example:
579 % 10 = 9
579 / 10 = 57
57 % 10 = 7
57 / 10 = 5
5 % 10 = 5
5 / 10 = 0
The above operations can be performed in a while loop that iterates until your number becomes 0.