Digit Summation
A while loop extracts each digit of an integer and adds it to a running total.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
int n = 432;
int res = 0;
while (n > 0) {
res += n % 10;
n /= 10;
}
System.out.print(res);
}
}
A
432
B
2
C
9
D
7
Question Leaderboard
Not enough data yet to show leaderboard.
