Could You Write A Program To Compute Sum Of Digits Of A Given Number? (Hint: Separate Digits One By One From The Number And Then Add)


2 Answers

Oddman Profile
Oddman answered
I don't know much about Java, so you'll have to translate it to the form you need. Basically, you want to find the digits one by one and add to a sum.

Let n be your input number, m is a working variable, sum is the total you are looking for; all are positive integers.
m = n; sum = 0; while(m>0){sum+=m%10; m/=10}

Answer Question