Question:
How do i prints hollow triangles using asterisks in java?
Code:
public class HollowTriangle { public static void main(String[] args) { int width = 10; for (int i = 0; i < width; i++) { for (int j = 0; j < width; j++) { if (i == 0) System.out.print("*"); else if(j == i) System.out.print("*"); else if(j == (width-1)) System.out.print("*"); else System.out.print(" "); } System.out.println(""); } } }
Output:
$ java HollowTriangle ********** * * * * * * * * * * * * * * ** *