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

Java Program to Print X Star Pattern

In this tutorial, we will see how to print the X 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 X 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 X Star Pattern

In this program, we will see how to print the X 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 a for loop to print the pattern.

  6. In the inner for loop iterates from j=1 to k and prints charter if j=i or j=k-i+1 displays “*”, else it displays space.

  7. This code will execute until the inner for loop condition is false, then it comes to the outer for loop, the for loop will execute until the condition i<=k is false.

  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 X Star Pattern Using For Loop
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();	 
        int k=n*2-1;
	    for(int i=1;i<=k;i++)
        {
	        for(int j=1;j<=k;j++)
            {      
                if(j==i || j==k-i+1)
                System.out.print("*");
                System.out.print(" ");
            }
	        System.out.println();
        }            
    }
}


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

Program 2: Java Program to Print the X Star Pattern

In this program, we will see how to print the X 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 a while loop to print the pattern.

  6. While loop checks the condition first then executes the code.

  7. First check the condition at while loop i.e i<=k, if it is true, then come to the inner while loop.

  8. In the inner while loop first checks the condition j<=k, then execute the code in the loop until the condition is false, then the cursor comes out of the inner loop and goes to the outer loop, this will continue until the condition i<=k is false.

  9. Display the result.

  10. Stop

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

//Java Program to Print the X Star Pattern Using While Loop
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();	 
        int i=1;
        int j;
	    int k=n*2-1;
	    while(i<=k)
        {
	        j=1;
		    while(j<=k) 
            {     
                if(j==i || j==k-i+1)
                System.out.print("*");
                System.out.print(" ");
		        j++;
            }
	    System.out.println();
	    i++;
       } 
    }
}


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

Program 3: Java Program to Print Star Pattern

In this program, we will see how to print the X 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 a do-while loop to print the pattern.

  6. First, execute the inner do-while loop.

  7. The code in the inner loop executes until the condition j<=k is false. It prints a character for j=i ,j=k-i+1.Other than these j values prints space.

  8. If the condition is false then the cursor comes to the outer do-while loop. The outer do loop execution will stop if the condition i<=k is false.

  9. Display the result.

  10. Stop

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

//Java Program to Print Star Pattern Using a do-while Loop
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();	 
        int i=1;
        int j;
	    int k=n*2-1;
	    do
        {
	        j=1;
            do
            {   
                if(j==i || j==k-i+1)
                System.out.print(“*”);
                System.out.print(" ");
		        j++;
            }
            while(j<=k);
	        System.out.println();
	        i++;
        }while(i<=k);      
    }
}


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