Pyramid pattern is a popular pattern. Its logic is similar to Mirrored Right Angle Triangle Pattern. Let us see the logic to create a pyramid easily.
public class Tech4Humans {
public static void main(String[] args) {
// length of triangle
int size = 5;
// Logic is similar to Mirrored Right Angle Triangle Pattern
for (int i = 0; i < size; i++) {
for (int j = 0; j < (size - i - 1); j++) {
System.out.print(" ");
}
for (int k = 0; k <= i; k++) {
System.out.print("* ");
}
System.out.println();
}
}
}
*
* *
* * *
* * * *
* * * * *
If you wish to debug code easily refer the article RIGHT WAY TO DEBUG CODE USING PEN AND PAPER.