[Java] Star (X) Pattern Java Program


In this “Print Star (X) Pattern” – We have written Java programs to print/draw Star (X) pattern by using different types of Java loops and you can execute and try these Java program through your IDE. This is most [100+] Frequently Asked Java Program on beginner level to check you Java Programming skills and Concept.

Pattern Example

This post covers following ways to print Star (X) Pattern:

  • Print Star (X) Pattern Java Program: Using for Loop
  • Print Star (X) Pattern Java Program: Using While Loop
  • Print Star (X) Pattern Java Program : Using Do-While Loop

Star (X) Pattern Java Program: For Loop

This Java Program is following the below logic:

  1. Inner for loop iterates from j=1 to k and prints characters if j=i or j=k-i+1 displays “*” (symbol), else display space.
  2. Inner for loop will execute until condition i<=k is false, then it comes to the outer for loop.
package _1_patterns.xstar;

import java.util.Scanner;

public class XStarPatternForLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawXStarPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawXStarPattern(int n, char c) {
		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(c);
				}
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}

Output

Enter Pattern Size : 
8
Enter Symbol : $
$              $ 
 $            $  
  $          $   
   $        $    
    $      $     
     $    $      
      $  $       
       $        
      $  $       
     $    $      
    $      $     
   $        $    
  $          $   
 $            $  
$              $ 

Star (X) Pattern Java Program: While Loop

This Java Program is following the below logic:

  1. While loop first checks the condition i.e i<=k, if it true, then it comes to the inner while loop.
  2. Inner while loop first checks the condition i.e j<=k, then it executes the logic in loop until the condition is false, then pointer come out of the inner loop and goes to the outer loop, this will continue until the condition i<=k is false.
package _1_patterns.xstar;

import java.util.Scanner;

public class XStarPatternWhileLoop {

	public static void main(String[] args) {

		Scanner sc = new Scanner(System.in);

		System.out.println("Enter Pattern Size : ");
		int size = sc.nextInt();

		System.out.print("Enter Symbol : ");
		char symbol = sc.next().charAt(0);

		drawXStarPattern(size, symbol);

		// Close Scanner
		sc.close();
	}

	private static void drawXStarPattern(int n, char c) {
		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(c);
				}
				System.out.print(" ");
				j++;
			}
			System.out.println();
			i++;
	}
}

Output

Enter Pattern Size : 
5
Enter Symbol : @
@        @ 
 @      @  
  @    @   
   @  @    
    @     
   @  @    
  @    @   
 @      @  
@        @ 

Star (X) Pattern Java Program: Do-While Loop

This Java Program is following the below logic:

  1. Inner do-while loop, first the code in the inner loop executes until the condition j<=k is false. It prints symbol for j=1, j=k-i+1. Other than these j values print space.
  2. If the condition false, then pointer comes to outer do-while loop and execute until the condition i<=k is false.
package _1_patterns.xstar;

import java.util.Scanner;

public class XStarPatternDoWhileLoop {
public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);

    System.out.println("Enter Pattern Size : ");
    int size = sc.nextInt();

    System.out.print("Enter Symbol : ");
    char symbol = sc.next().charAt(0);

    drawXStarPattern(size, symbol);

    // Close Scanner
    sc.close();
}

private static void drawXStarPattern(int n, char c) {
        int i = 1;
		int j;
		int k = n * 2 - 1;

		do {
			j = 1;
			do {
				if (j == i || j == k - i + 1) {
					System.out.print(c);
				}
				System.out.print(" ");
				j++;

			} while (j <= k);
			System.out.println();
			i++;
		} while (i <= k);
}
}

Output

Enter Pattern Size : 
6
Enter Symbol : ^
^          ^ 
 ^        ^  
  ^      ^   
   ^    ^    
    ^  ^     
     ^      
    ^  ^     
   ^    ^    
  ^      ^   
 ^        ^  
^          ^ 

Hope this post helps you to implement the Start (X) Pattern through Java Program. Please share your comments.

Happy Learning !!!

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s