Nested Summation
A program uses nested loops to calculate a total based on the sum of loop indices.
What is printed when the following Java program is compiled and run?
public class QuestionTrace {
public static void main(String[] args) {
int total = 0;
for (int i = 1; i <= 3; i++) {
for (int j = i; j <= 3; j++) {
total += i + j;
}
}
System.out.println(total);
}
}
A
21
B
24
C
18
D
27
Question Leaderboard
Not enough data yet to show leaderboard.
