Solved programming problem

Star Pattern in Java.

Source code

package pkg1.a;

public class C {

    public static void main(String[] args) {

        int rows = 5;

        for (int i = 1; i <= rows; i++) {

            for (int j = 1; j < i; j++) {

                System.out.print(" ");

            }

                 

            for (int j = 1; j <= (rows * 2 - (2* i - 1)); j++) {

                if (i == 1 || j == 1 || j == (rows * 2 - (2*i - 1))) {

                    if(i==1 &&j%2!=0)

                    System.out.print(" ");

                    else

                    {

                     System.out.print("*");

                    }

                } else {

                    System.out.print(" ");

                }

            }

            System.out.println("");

        }

    }

}

Output

Output:

 

Join the discussion

Add your comment