Java Program to Print Isosceles Triangle Star Pattern

  • Java program to print isosceles triangle star pattern using for loop.
Sample Output,
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
* 

Java program to print isosceles star pattern

package com.tcc.java.programs;

public class StarPatternThree {
    public static void main(String[] arg) {
        int i, j, c = 0;
        int rows = 9;
        // Printing Upper half of pattern
        // Outer for loop prints one row in every iteration
        for (i = 0; i < rows / 2 + 1; i++) {
            /* Inner for loop prints n * characters for nth row */
            for (j = 1; j <= i + 1; j++) {
                System.out.print("* ");
            }
            System.out.print("\n");
        }

        // Printing lower half of pattern
        // Outer for loop prints one row in every iteration
        // Number of stars decreases with every consecutive rows
        for (i = 0; i <= rows / 2; i++) {
            /* for Nth row, it prints n-i+1 star character */
            for (j = 1; j <= rows / 2 - i; j++) {
                System.out.print("* ");
            }
            System.out.print("\n");
        }
    }
}
Output
* 
* * 
* * * 
* * * * 
* * * * * 
* * * * 
* * * 
* * 
*