Filled
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
class TriFilled{ public static void main(String args[]){ // loop for rows for(int i=1;i<=5;i++){ // loop for first spaces for(int j=i;j<=5;j++){ System.out.print(" "); } // loop for first half of stars for(int k=1;k<=i;k++){ System.out.print("*"); } //loop for second half of stars for(int l=2;l<=i;l++){ System.out.print("*"); } // break the line System.out.println(); } } } |
Outlined
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
class TriOutline{ public static void main(String args[]){ int height = 5; // loop for rows for(int i=1;i<=height;i++){ // loop for first spaces for(int j=i;j<=height;j++){ System.out.print(" "); } // loop for first half of stars for(int k=1;k<=i;k++){ // put star when k starts or when we have last row if(k==1 || i==height){ System.out.print("*"); } else{ System.out.print(" "); } } //loop for second half of stars for(int l=2;l<=i;l++){ // put star when l ends or when we have last row if(l==i || i==height){ System.out.print("*"); } else{ System.out.print(" "); } } // break the line System.out.println(); } } } |