Open In App

Java Program to Print Square Star Pattern

Improve
Improve
Like Article
Like
Save
Share
Report

Here, we will implement a Java program to print the square star pattern. We will print the square star pattern with diagonals and without diagonals. 

Example:

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

Approach:

Step 1: Input number of rows and columns.

Step 2: For rows of rectangle run the outer loop from 1 to rows.

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

Step 3: For the column of the rectangle run the inner loop from 1 to columns.

for (j = 1; j < = columns; j++)

Step 4: Print star for first or last row or for first or last column, otherwise print blank space.

Step 5: After printing all columns of a row, print the newline after the inner loop.

Example 1: Star square pattern without diagonals

Java




// Java Program to Print Square Pattern
// Case 1: Hollow rectangle
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // To print hollow rectangle
    static void print_rectangle(int k, int l)
    {
        int a, b;
 
        // Nested for loops for iterations
 
        // Outer loop for rows
        for (a = 1; a <= k; a++) {
            // Inner loop for columns
            for (b = 1; b <= l; b++) {
                // Condition check using logical OR operator
                // over rows and columns positions
                // if found at circumference of rectangle
                if (a == 1 || a == k || b == 1 || b == l)
 
                    // Print the star pattern
                    System.out.print("*");
                else
 
                    // Rest inside square print the empty
                    // spaces
                    System.out.print(" ");
            }
 
            // By now we are done with only 1 row so
            // New line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // Declaring and initializing rows and columns
        // For square row = columns
 
        // Custom input initialization values
        int rows = 8, columns = 22;
 
        // Calling the method1 to print hollow rectangle
        // pattern
        print_rectangle(rows, columns);
    }
}


Output

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

Time complexity: O(R*C) for given no of rows R and columns C

Auxiliary space: O(1) because it is using constant space
 

Now taking it a step further as in above we simply inserted empty spaces inside the rectangle. In mathematics, we already have come across diagonals, so we can print them too in the above case.

Example 2: Star square pattern with diagonals 

Java




// Java Program to Print Square Star pattern
// Case: Primary and secondary diagonal Rectangle Pattern
import java.io.*;
 
// Mai class
class GFG {
 
    // Method 1
    // To print square with primary and secondary diagonal
    static void print_squaredi(int k)
    {
        int a, b;
 
        // Nested 2 for loops for Matrix printing
 
        // Outer loop for rows
        for (a = 1; a <= k; a++) {
            // Inner loop for columns
            for (b = 1; b <= k; b++) {
                // Condition check using OR operator where
                // 1. Printing star as per first 4 checks
                // on the circumference of rectangle
                // 2. Fifth check is for diagonals
                if (a == 1 || a == k || b == 1 || b == k
                    || a == b || b == (k - a + 1))
 
                    // Print the star pattern
                    System.out.print("*");
                else
 
                    // Print the white spaces
                    System.out.print(" ");
            }
 
            // By now we are over with one row so
            // new line
            System.out.println();
        }
    }
 
    // Method 2
    // Main driver method
    public static void main(String args[])
    {
        // This time taking square so only one variable
        // needs to be declared
        // Custom input entry
        int rows = 12;
 
        // calling the method1 to print
        // square pattern with diagonal
        print_squaredi(rows);
    }
}


Output

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

Time complexity: O(N2) where N is given input, no of rows
Auxiliary Space : O(1)
 



Last Updated : 01 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads