I need help with a program where I have to find the sum of the digits of an integer.
For example, for the integer 32, the sum of the digits of the integer is: 3+2 = 5
Or, for the integer 321, the sum of the digits of the integer is: 3+2+1 = 6
One person gave me this code (but did not explain):
class Digits { public static void main(String args[]) { int num = 25; int sum = 0; while (num > 0) { sum = (sum + num) % 10; num = num / 10; } System.out.println("The sum of the digits of 25 is: " + sum); } }
However, I'm having trouble understanding what's going on in the while statement. BTW, i've even tried it with the numbers above (32 and 321), but I still don't understand the input?