Java Program to Print Square Star Pattern

Write a Java Program to Print Square Star Pattern using For Loop, and While Loop with example.

Java Program to Print Square Star Pattern using For Loop

This program allows the user to enter any side of a square (integer value). Next, this Java program displays the square star pattern until it reaches the user-specified rows and columns.

// Java Program to Print Square Star Pattern
import java.util.Scanner;

public class SquareStar1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int side, i, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Side of a Square : ");
		side = sc.nextInt();	
			
		for(i = 1; i <= side; i++)
		{
			for(j = 1; j <= side; j++)
			{
				System.out.print("*"); 
			}
			System.out.print("\n"); 
		}	
	}
}
Java Program to Print Square Star Pattern 1

First, the For loop is to iterate from 1 to user-entered side. Next, we used Nested For Loop to iterate j from 1 to user entered value (side). In this Java example, User entered value: side  = 5

First For Loop – First Iteration: for(i = 1; i <= 5; i++)
Condition is True. So, it enter into the second For Loop

Second For Loop – First Iteration: for(j = 1; 1 <= 5; 1++)
Condition is True. So, * inside the system.out.println printed.

Second For Loop – Second Iteration: for(j = 2; 2 <= 5; 2++)
Condition is True. So, * inside the system.out.println printed.

Second For Loop repeats the process until j reaches 6. Because if j is 6, the condition fails so, it exits from the second loop.

First For Loop – Second Iteration: for(i = 2; i <= 5; 2++)
Condition is True. So, it enters into the second For Loop. Repeat the above process until i reaches 6.

Java Program to Print Square Star Pattern using While Loop

This Java program to return the square star pattern is the same as the above example, but we are using the While Loop.

// Java Program to Print Square Star Pattern
import java.util.Scanner;

public class SquareStar2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int side, i = 1, j;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Side of a Square : ");
		side = sc.nextInt();	
			
		while(i <= side)
		{
			j = 1;
			while(j <= side)
			{
				System.out.print("* "); 
				j++;
			}
			i++;
			System.out.print("\n"); 
		}	
	}
}

Java Square Star Pattern using a While loop output

 Please Enter any Side of a Square : 8
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 
* * * * * * * * 

Java Program to Print Square Pattern of Custom Character

This Java program allows the user to enter any character and then prints that character in a square pattern.

// Java Program to Print Square Star Pattern
import java.util.Scanner;

public class SquareStar3 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int side, i, j;
		char ch;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter any Side of a Square : ");
		side = sc.nextInt();	
		
		System.out.print(" Please Enter any Character : ");
		ch = sc.next().charAt(0);	
			
		for(i = 1; i <= side; i++)
		{
			for(j = 1; j <= side; j++)
			{
				System.out.print(ch + " "); 
			}
			System.out.print("\n"); 
		}	
	}
}
 Please Enter any Side of a Square : 10
 Please Enter any Character : $
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $ 
$ $ $ $ $ $ $ $ $ $