Java Program to Print X inside Rectangle Star Pattern

In this Java pattern program, we show the steps to print the X pattern inside a rectangle star shape using for loop, while loop, and functions. The below program accepts the user-entered rows and uses the nested for loop and if else to traverse the rows and columns. Next, the program will print the X pattern inside the rectangle shape of the stars.

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Rows = ");
int rows = sc.nextInt();

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (i == j || i + j == rows - 1)
{
if (i + j == rows - 1)
{
System.out.printf("/");
}
else {
System.out.printf("\\");
}
}
else
{
System.out.printf("*");
}
}
System.out.println();
}
}
}
Enter Rows = 14
\************/
*\**********/*
**\********/**
***\******/***
****\****/****
*****\**/*****
******\/******
******/\******
*****/**\*****
****/****\****
***/******\***
**/********\**
*/**********\*
/************\

Within this program, we replaced the for loop with a while loop to traverse the rows and columns and print the X shape inside the rectangle stars pattern. For more star pattern programs, please click here.

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);
int rows, i, j;

System.out.print("Enter Rows = ");
rows = sc.nextInt();

System.out.print("Enter Character = ");
char a = sc.next().charAt(0);

i = 0 ;
while (i < rows )
{
j = 0 ;
while (j < rows )
{
if (i == j || i + j == rows - 1)
{
if (i + j == rows - 1)
{
System.out.printf("/");
}
else
{
System.out.printf("\\");
}
}
else
{
System.out.printf("%c", a);
}
j++;
}
System.out.println();
i++;
}
}
}
Enter Rows = 16
Enter Character = #
\##############/
#\############/#
##\##########/##
###\########/###
####\######/####
#####\####/#####
######\##/######
#######\/#######
#######/\#######
######/##\######
#####/####\#####
####/######\####
###/########\###
##/##########\##
#/############\#
/##############\

In this Java pattern program, we created the XinRectangleStars function to print the X shape inside rectangle stars pattern.

import java.util.Scanner;

public class Example
{
private static Scanner sc;

public static void main(String[] args)
{
sc = new Scanner(System.in);

System.out.print("Enter Rows = ");
int rows = sc.nextInt();

XinRectangleStars(rows);
}

public static void XinRectangleStars(int rows)
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
if (i == j || i + j == rows - 1)
{
if (i + j == rows - 1)
{
System.out.printf("/");
}
else {
System.out.printf("\\");
}
}
else
{
System.out.printf("*");
}
}
System.out.println();
}
}
}
Print X inside Rectangle Star Pattern