Return the sum of the digits of the positive integer n.
digitSum(4071) returns 12.
9 strips: B1 to B9
n /= 10; B1sum += n % 10; B2} B3} B4sum += n / 10; B5while (n > 0) { B6int sum = 0; B7return sum; B8public static int digitSum(int n) { B9Order: B9, B7, B6, B2, B1, B3, B8, B4. Extra: B5
public static int digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
} Extra strip: sum += n / 10;. n / 10 is the number with its last digit cut off. The last digit is n % 10.