Java Program to Print X Star Pattern

Write a Java Program to print X star pattern using for loop. This Java X star example uses if condition within the nested for loops to iterate the rows.

package ShapePrograms;

import java.util.Scanner;

public class XPattern1 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);	
		
		System.out.print("Please Enter X Pattern Rows = ");
		int rows = sc.nextInt();	
		
		System.out.println("---- Printing X Pattern of Stars ------");
		
		int k = rows * 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();
		}
	}
}
Java Program to Print X Star Pattern 1

In this Java X star pattern program, we replaced the for loop with the while loop.

package ShapePrograms;

import java.util.Scanner;

public class XPattern2 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int  rows,  j;
		sc = new Scanner(System.in);	
		
		System.out.print("Please Enter X Pattern Rows = ");
		rows = sc.nextInt();
		
		System.out.println("---- Printing X Pattern of Stars ------");
		int k = rows * 2 - 1;
		int i = 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++;
		}
	}
}
Please Enter X Pattern Rows = 7
---- Printing X Pattern of Stars ------
*            * 
 *          *  
  *        *   
   *      *    
    *    *     
     *  *      
      *       
     *  *      
    *    *     
   *      *    
  *        *   
 *          *  
*            * 

Java Program to Print X Star Pattern using do while loop

package ShapePrograms;

import java.util.Scanner;

public class XPattern3 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		int  rows,  j;
		sc = new Scanner(System.in);	
		
		System.out.print("Please Enter X Pattern Rows = ");
		rows = sc.nextInt();
		
		System.out.println("---- Printing X Pattern of Stars ------");
		int k = rows * 2 - 1;
		int i = 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);
	}
}
Please Enter X Pattern Rows = 9
---- Printing X Pattern of Stars ------
*                * 
 *              *  
  *            *   
   *          *    
    *        *     
     *      *      
      *    *       
       *  *        
        *         
       *  *        
      *    *       
     *      *      
    *        *     
   *          *    
  *            *   
 *              *  
*                * 

In this Java example, the XPattern function prints the X pattern of a given symbol.

package ShapePrograms;

import java.util.Scanner;

public class XPattern4 {
	private static Scanner sc;
	
	public static void main(String[] args) {
		sc = new Scanner(System.in);
		
		System.out.print("Please Enter X Pattern Rows = ");
		int rows = sc.nextInt();
		
		System.out.print("Enter Character for X Pattern = ");
		char ch = sc.next().charAt(0);
		
		System.out.println("---- Printing X Pattern ------");
		XPattern(rows, ch);
	}
	
	public static void XPattern(int rows, char ch) {
		int k = rows * 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(ch);
				}
				System.out.print(" ");
			}
			System.out.println();
		}
	}
}
Please Enter X Pattern Rows = 10
Enter Character for X Pattern = #
---- Printing X Pattern ------
#                  # 
 #                #  
  #              #   
   #            #    
    #          #     
     #        #      
      #      #       
       #    #        
        #  #         
         #          
        #  #         
       #    #        
      #      #       
     #        #      
    #          #     
   #            #    
  #              #   
 #                #  
#                  #