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

16 comments:

  1. Thank you really...

    ReplyDelete
  2. best induction cooktop. you could think of each layer as one big loosely coupled component with strict design rules of access between layers.

    ReplyDelete
  3. sad shayari. woman in the universe who has ever truly tempted him.

    ReplyDelete
  4. I read this post two times, I like it so much, please try to keep posting & Let me introduce other material that may be good for our community.


    "mern stack course in bangalore,
    mern stack developer course in bangalore,
    mern stack training in bangalore"

    ReplyDelete