C Program to Print X inside a Rectangle Number Pattern

In this article, we will show how to write a C program to print the X shape inside a rectangle number pattern using for loop, while loop, and functions. The below example allows the user to enter the total number of rows, and the nested for loop iterates them from start to end. Next, the program will print the X shape inside a rectangle number pattern.

#include <stdio.h>

int main()
{
int rows;

printf("Enter Rows = ");
scanf("%d",&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)
{
printf("/");
}
else
{
printf("\\");
}
}
else
{
printf("%d", i);
}
}
printf("\n");
}

return 0;
}
Enter Rows = 9
\0000000/
1\11111/1
22\222/22
333\3/333
4444/4444
555/5\555
66/666\66
7/77777\7
/8888888\

Instead of a for loop, this program uses a while loop to iterate the rectangle shape of rows and columns and prints the x pattern inside a rectangle number shape. For more number programs, click here.

#include <stdio.h>

int main()
{
int rows, i, j;

printf("Enter Rows = ");
scanf("%d", &rows);

i = 0;
while (i < rows)
{
j = 0;
while (j < rows)
{
if (i == j || i + j == rows - 1)
{
if (i + j == rows - 1)
{
printf("/");
}
else {
printf("\\");
}
}
else
{
printf("%d", i);
}
j++;
}
printf("\n");
i++;
}
return 0;
}
Enter 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\

In this C program, we create an XshapeinRectangleNumbers function that accepts the user entered rows and prints the rectangle number pattern with an X shape inside it as diagonal.

#include <stdio.h>

void XshapeinRectangleNumbers(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)
{
printf("/");
}
else
{
printf("\\");
}
}
else
{
printf("%d", i);
}
}
printf("\n");
}
}

int main() {
int rows;

printf("Enter Rows = ");
scanf("%d", &rows);

XshapeinRectangleNumbers(rows);
return 0;
}
C Program to Print X inside a Rectangle Number Pattern