31 thoughts on “Pyramid Pattern in Java

  1. Can u help I am trying to write a program using a while loop that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input

  2. No offense intended:

    A very long-winded and indirect method. Much simpler method would be:

    public class Pattern
    {
    public static void pattern()
    {
    int a=4, b=1;
    for (int i=1; i<=5; i++)
    {
    for (int j=1; j<=a; j++)
    {
    System.out.print(" ");
    }

    for (int n=1; n<=b; n++)
    {
    System.out.print("*");
    }

    System.out.println();
    a–;
    b+=2;
    }
    }
    }

  3. This is the code for a Christmas tree.
    static public void printStarTriangle(int linecount) {
    for(int triangles = 0;triangles<1; triangles++) {
    for(int line = 0; line < linecount-3; line+=1) {
    System.out.printf("%s", " ");
    for(int spaces = 0; spaces < linecount-line;spaces++) {
    System.out.printf("%s"," ");
    }
    for(int stars = 0; stars < 2*line+1; stars++) {
    System.out.printf("%s", "*");
    }
    System.out.printf("n");
    }
    for(int line = 0; line < linecount; line+=1) {
    System.out.printf("%s", " ");
    for(int spaces = 0; spaces < linecount-line;spaces++) {
    System.out.printf("%s"," ");
    }
    for(int stars = 0; stars < 2*line+1; stars++) {
    System.out.printf("%s", "*");
    }
    System.out.printf("n");
    }
    }
    System.out.printf("%sn", " ***");
    System.out.printf("%sn", " ***");
    System.out.printf("%sn", " ***");
    System.out.printf("%sn", " ***");
    System.out.printf("%sn", " ***");
    }}

Leave a Reply

Your email address will not be published. Required fields are marked *