Printing a Triangle with Nested Loops
Which of the following code segments, when executed, will print the right triangle pattern of asterisks shown below?
*
**
***
****
A
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 4; j++) {
System.out.print(“*”);
}
System.out.println();
}
B
for (int i = 1; i <= 4; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(“*”);
}
System.out.println();
}
C
for (int i = 1; i <= 4; i++) {
for (int j = 4; j >= i; j–) {
System.out.print(“*”);
}
System.out.println();
}
D
for (int i = 4; i >= 1; i–) {
for (int j = 1; j <= i; j++) {
System.out.print(“*”);
}
System.out.println();
}
Question Leaderboard
Not enough data yet to show leaderboard.
