If you have any query feel free to chat us!
Happy Coding! Happy Learning!
Here's an example of how to create a pyramid pattern using loops in Java:
markdownCopy code *
***
*****
*******
*********
javaCopy codepublic class PyramidPattern {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++) {
System.out.print(" ");
}
for (int k = 1; k <= 2 * i - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
In the program above, we use three nested loops to create the pyramid pattern. The outer loop runs n
(5 in this case) times, representing the number of rows in the pyramid. The first inner loop prints spaces to create the left-aligned pyramid shape. The second inner loop prints the asterisks to create the triangle part of the pyramid. The number of asterisks in each row is determined by 2 * i - 1
, where i
represents the current row number.
You can adjust the value of n
to create pyramid patterns of different sizes. The larger the value of n
, the larger the pyramid will be.
Under Section 107 of the copyright act 1976, allowance is made for fair use for purposes such as criticism, comment, news reporting, scholarship, and research. Fair use is a use permitted by copyright statute that might otherwise be infringing. Non-profit, educational, or personal use tips the balance in favor of fair use.
Comments: 0