Java code to print Asterisk Pyramid

Below java code snippet will print the asterisk(*) pyramid(triangle) of 5 lines.


public class PrintPyramidStar {

    public static void main(String args[]) {

        int c = 1;
        for (int i = 1; i <= 5; i++) {
            for (int j = i; j < 5; j++) {
                System.out.print(" ");
            }
            for (int k = 1; k <= c; k++) {
                if (k % 2 == 0)
                    System.out.print(" ");
                else
                    System.out.print("*");
            }
            System.out.println();
            c += 2;
        }
    }
}


Output:

    *
   * *
  * * *
 * * * *
* * * * *

You may also like
Diamond Pattern

3 comments:

  1. Thank you really...

    ReplyDelete
  2. I have read your blog its very attractive and impressive. I like it your blog.

    Java Training in Chennai Core Java Training in Chennai Core Java Training in Chennai

    Java Online Training Java Online Training JavaEE Training in Chennai Java EE Training in Chennai

    ReplyDelete