for a school project, I am trying to make an inverted half pyramid
my code is currently this
JavaScript
x
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) {
for(int j = 1; j <= i; ++j) {
System.out.print(j + " ");
}
System.out.println();
}
}
with this output:
JavaScript
12345
1234
123
12
1
desired output:
JavaScript
54321
=4321
==321
===21
====1
Advertisement
Answer
Update (based on the updated requirement):
You need a loop to print the =
equal to (rows
– row number
) times.
JavaScript
public class Main {
public static void main(String[] args) {
int rows = 5;
for (int i = rows; i >= 1; --i) {
for (int j = i; j < rows; j++) {
System.out.print("=");
}
for (int j = i; j >= 1; --j) {
System.out.print(j);
}
System.out.println();
}
}
}
Output:
JavaScript
54321
=4321
==321
===21
====1
Original answer:
Your inner loop should be
JavaScript
for (int j = i; j >= 1; --j)
i.e. for each row, it should start with the row number (i.e. i
) and go down up to 1
.