Java Inverted Triangle: Quick Code Guide
Here is an example code in Java for implementing an inverted right-angled triangle.
public class InvertedRightAngleTriangle {
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("* ");
}
System.out.println();
}
}
}
In this example, we set a variable rows to represent the number of rows in an inverted right-angled triangle. Then, we use nested for loops to print the pattern of the inverted right-angled triangle. The outer loop controls the number of rows, while the inner loop controls the number of asterisks printed in each row. Finally, the output is as follows:
* * * * *
* * * *
* * *
* *
*