Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print star using star pattern Java

Tags:

java

I want to print the below pattern

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

I am able to print the two outer pyramids in the pattern using the below code:

    public static void main(String[] args) {
    int n = 8;
    int i, j;

    for (i = 1; i <= n; i++) {
        for (j = i; j < n; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }

        System.out.print("\n");

    }
    for (i = n; i >= 1; i--) {
        for (j = i; j < n; j++) {
            System.out.print(" ");
        }
        for (j = 1; j <= (2 * i - 1); j++) {
            if (i == n || j == 1 || j == (2 * i - 1)) {
                System.out.print("*");
            } else {
                System.out.print(" ");
            }
        }
        System.out.print("\n");
    }
}

This prints the pattern as

       *               
      * *              
     *   *             
    *     *             
   *       *           
  *         *              
 *           *
***************
***************
 *           *
  *         *
   *       *
    *     *
     *   *
      * *
       *``

But how to combine these two to form a star shape?

like image 665
user3458666 Avatar asked Aug 09 '26 05:08

user3458666


2 Answers

Since you didn't specify what your restrictions were...

public static void main(String[] args) {
    System.out.println("       *       ");
    System.out.println("***************");
    System.out.println(" *   *   *   * ");
    System.out.println("  * *     * *  ");
    System.out.println("   *       *   ");
    System.out.println("  * *     * *  ");
    System.out.println(" *   *   *   * ");
    System.out.println("***************");
    System.out.println("       *       ");
}

Now obviously that is not what you intend to do, but next time it would be of help if you are more specific about your requirements, i.e.: "I have to do it using only for loops", "I'm not allowed to print more than a character at a time", "the code must be able to print any star of a given width", etc.
Because if none of those restrictions are in place, then the best way is just to print the ASCII lines like above.

like image 133
walen Avatar answered Aug 11 '26 18:08

walen


You could temporarily store the printing in a 2D char array and modify it twice, then use a for loop to print the array.

public static void main(String[] args) {
int n = 8;
char[][] temp = new char[][];
Arrays.fill(temp, ' ');
int i, j;

for (i = 1; i <= n; i++) {
    for (j = 1; j <= (2 * i - 1); j++) {
        if (i == n || j == 1 || j == (2 * i - 1)) {
            temp[i][j] = '*';
        }
    }
}
for (i = n; i >= 1; i--) {
    for (j = 1; j <= (2 * i - 1); j++) {
        if (i == n || j == 1 || j == (2 * i - 1)) {
            temp[i][j] = '*';
        }
    }
}

for (i = 0; i < n; i++) {
    for (j = 0; j < n; j++)
        System.out.print(temp[i][j]);
    System.out.println();
}

}

like image 44
theEpsilon Avatar answered Aug 11 '26 20:08

theEpsilon