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

Java Program to Print the Plus Star Pattern

In this tutorial, we will see how to print the plus star pattern in Java. First, we will ask the user to initialize the number of rows. Then, we will use loops to print the plus 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: 6

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: Print the Plus Star Pattern

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

Algorithm:

  1. Start
  2. Create an instance of the Scanner class.
  3. Declare variables to store the number of rows and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use a for loop to print the pattern.
  6. First, check the condition at for loop, if it is true then check the “if “condition, if the “if” condition is true then execute the first inner for loop otherwise execute the else part i.e second inner for loop.
  7. The code will execute until the condition at for loop is false.
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Plus 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();	 
	for(int i=1;i<=n*2-1;i++)
    {
        if(i!=n)
	    for(int j=1;j<=n;j++)
        {      
            if(j==n)
            System.out.print("*");
            System.out.print(" ");
        }
	   else
		for(int j=1;j<=n*2-1;j++)
        {
            System.out.print("*");
        }
	   System.out.println();
       }            
    }
}


Enter the number of rows: 8
*
*
*
*
*
*
*
***************
*
*
*
*
*
*
*

Program 2: Print the Plus Star Pattern

In this program, we will see how to print the plus 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 and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use a while loop to print the pattern.
  6. First check the condition at while loop, if it is true then check the “if “condition, if the “if” condition is true then execute the first inner while loop otherwise executes the else part i.e second inner while loop.
  7. The code will execute until the condition at while loop is false i.e 1<=n*2-1
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Plus 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*2-1)
        {
            if(i!=n)
	        {
                j=1;
		        while(j<=n)
                {      
                    if(j==n)
                    System.out.print("*");
                    System.out.print(" ");
                    j++ ;
                }
          }
	      else
	      {
        	    j=1;
		        while(j<=n*2-1)
                {
                   System.out.print("*");
			       j++;
                }
    	   }
	         System.out.println();
	         i++; 
       }  
    }
}


Enter the number of rows: 6
*
*
*
*
*
***********
*
*
*
*
*

Program 3: Print the Plus Star Pattern

In this program, we will see how to print the plus 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 and the pattern symbol.
  4. Ask the user to initialize these variables.
  5. Use a do-while loop to print the pattern.
  6. First, check the condition at the do-while loop, if it is true then check the “if “condition, if the “if” condition is true then execute the first inner do-while loop otherwise execute the else part i.e second inner do-while loop.
  7. The code will execute until the condition at do-while loop is false i.e 1<=n*2-1
  8. Display the result.
  9. Stop

The below example illustrates the implementation of the above algorithm.

//Java Program to Print the Plus 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
        {
            if(i!=n)
	        {
                j=1;
		        do
                {      
                    if(j==n)
                    System.out.print("*");
                    System.out.print(" ");
                    j++ ;
                }while(j<=n);
            }
	        else
	        {
        	    j=1;
		        do
                {
                   System.out.print("*");
			       j++;
                }while(j<=n*2-1);
    	     }
	         System.out.println();
	         i++; 
       }while(i<=n*2-1);
    }
}


Enter the number of rows: 6
*
*
*
*
*
***********
*
*
*
*
*