Hurry! Try our new Interactive Courses for FREE. 🥳   🚀

Java Program to Print the Square Star Pattern

In this tutorial, we will see how to print the square star pattern in java First, we will ask the user to initialize the number of rows. Then, we will use different loops to print the square star pattern. But before moving further, if you are not familiar with the concept of the loops in java, then do check the article on Loops in Java.

Input: Enter the number of rows: 7

Output: The pattern is:

*******

*******

*******

*******

*******

*******

*******

This can be done by using the following methods:

Approach 1: Using a For Loop

Approach 2: Using a While loop

Approach 3: Using a do-while loop

Let us look at each of these approaches for a better understanding.

Program 1: Java Program to Print the Square Star Pattern

In this program, we will see how to print the square star pattern in java using for loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows.

  4. Ask the user to initialize the number of rows.

  5. Use two for loops to print the pattern.

  6. Use the first for loop to print the * in each row.

  7. Use the second for loop to print the * in each column.

  8. Display the result.

  9. Stop.

Let us look at the below example to understand the implementation of the above algorithm.

//Java Program to Print the Square Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	 
        for(int i = 1; i <= n; i++)
		{
			for(int j = 1; j <= n; j++)
			{
				System.out.print("*"); 
			}
			System.out.print("\n"); 
		}	     
    }
}


Enter the number of rows: 7
*******
*******
*******
*******
*******
*******
*******

Program 2: Java Program to Print the Square Star Pattern

In this program, we will see how to print the square star pattern in java using a while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows.

  4. Ask the user to initialize the number of rows.

  5. Use two while loops to print the pattern.

  6. Declare two loop variables for each loop.

  7. Initialize the first loop variable to 1.

  8. Use the outer while loop for rows.

  9. Initialize the second loop variable to 1.

  10. The inner while loop for columns.

  11. Print the * if the condition satisfies.

  12. Display the result.

  13. Stop

Let us look at the below example to understand the implementation of the above algorithm.

//Java Program to Print the Square Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	
	    int i=1;
	    int j;
        while(i <= n)
		{
			j = 1;
			while(j <= n)
			{
				System.out.print("*"); 
				j++;
			}
			i++;
			System.out.print("\n"); 
		}	     
    }
}


Enter the number of rows: 7
*******
*******
*******
*******
*******
*******
*******

Program 3: Java Program to Print the Square Star Pattern

In this program, we will see how to print the square star pattern in java using a do-while loop.

Algorithm:

  1. Start

  2. Create an instance of the Scanner class.

  3. Declare variables to store the number of rows.

  4. Ask the user to initialize the number of rows.

  5. Use two do-while loops to print the pattern.

  6. Declare two loop variables for each loop.

  7. Initialize the first loop variable to 1.

  8. Use the outer do-while loop for rows.

  9. Initialize the second loop variable to 1.

  10. The inner do-while loop for columns.

  11. Print the * if the condition satisfies.

  12. Display the result.

  13. Stop

Let us look at the below example to understand the implementation of the above algorithm.

//Java Program to Print the Square Star Pattern
import java.util.Scanner;
public class Main
{
    public static void main(String[] args)
    {
        //Take input from the user
	    Scanner sc=new Scanner(System.in);
	    System.out.println("Enter the number of rows: ");
	    int n=sc.nextInt();	
	    int i=1;
	    int j;
        do
		{
			j = 1;
			do
			{
				System.out.print("*"); 
				j++;
			}while(j <= n);
			i++;
			System.out.print("\n"); 
		} while(i <= n);	     
    }
}


Enter the number of rows: 5
*****
*****
*****
*****
*****