Print Square Pattern In Java

You are currently viewing Print Square Pattern In Java

In this article, You are going to know the concept of Print Square Pattern In java program. We can give the rows and columns values to print square pattern in java program with stars. Let’s see the article to see the possibilities here.

Print Square Pattern In java

We can see the first program to perform a task below:

// Java Program to Print Square Pattern
// Case 1: Hollow rectangle

// Importing input output classes
import java.io.*;

// Main class
class CD {

	// 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);
	}
}

Explanation

Firstly, We need to import the java.io (input/output) class into this program. Secondly, The nested for loop will use to iterate the rows and columns to make the square pattern.

Thirdly, The logical OR operator will help to check whether the conditions are true or not. If it is true then it will print square pattern with stars. Otherwise, It will print as empty space.

After that, We have to declare the rows and columns values to print square or a hollow rectangle using java program. Finally, We will see the output on the screen using the system.out.printIn() function.

Output

The output of the print square pattern in java will show below for your reference.

**********************
*                    *
*                    *
*                    *
*                    *
*                    *
*                    *
**********************
Read Also : How to Square a Number In Java?

Print Square Pattern In java

We can see the second example program to print square with diagonal using java here.

// 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 rinting

		// 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);
	}
}

Execution

As usual, We have to import the java.io (input/output) class into this program. After that, We will use the nested for loop to matrix rinting. If the four steps of the conditions were satisfied then it will print square pattern with stars.

In addition, If the fifth were done then it will print the square pattern with diagonal. Later, We will get the output after applying the values of rows. Finally, The output will display using the system.out.printIn() function.

Output

The output of the print square pattern in java will be provided below for your reference.

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

Final Thoughts

I hope this Java article helps you to do “ Print Square Pattern in Java ”. If you have any doubts regarding this topic then please let us know via the comment section. We will help you. Share this article with other Java developers. Thank You!

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply