Java Program to Print X inside Rectangle Numbers Pattern

In this Java pattern program, we show the steps to print the X shape inside a rectangle numbers pattern 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 shape inside a rectangle pattern of numbers.

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 Numbers of Rows = ");
rows = sc.nextInt();

for (i = 0 ; i < rows; i++ )
{
for (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("%d", i);
}
}
System.out.println();
}
}
}
Enter Numbers of Rows = 10
\00000000/
1\111111/1
22\2222/22
333\33/333
4444\/4444
5555/\5555
666/66\666
77/7777\77
8/888888\8
/99999999\

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 numbers pattern. For more Number 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 Numbers of Rows = ");
rows = sc.nextInt();

for (i = 0 ; i < rows; i++ )
{
for (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("%d", i);
}
}
System.out.println();
}
}
}
Enter Numbers of Rows = 8
\000000/
1\1111/1
22\22/22
333\/333
444/\444
55/55\55
6/6666\6
/777777\

In this Java pattern program, we created the XinRectangleNumbers function to print the X shape inside the rectangle numbers 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 Numbers of Rows = ");
int rows = sc.nextInt();

XinRectangleNumbers(rows);

}
public static void XinRectangleNumbers(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("%d", i);
}
}
System.out.println();
}
}
}
Print X inside Rectangle Numbers Pattern