If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here's an example of how to create an inverted triangle using loops in Java:
markdownCopy code
*****
****
***
**
*
javaCopy code
public class InvertedTriangle {
public static void main(String[] args) {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
In the program above, we use two nested loops to create the inverted triangle pattern. The outer loop runs from n
(5 in this case) down to 1, and the inner loop prints the appropriate number of asterisks for each row. As the outer loop decrements, the number of asterisks in each row decreases, resulting in the inverted triangle.
You can adjust the value of n
to control the size of the inverted triangle and create different patterns.
Comments: 0