The number X pattern draws the two diagonals of a square with numbers, so they cross in the middle. Each number is its column number, which makes the X read 1 to 5 across the top and bottom rows.
Quick answer: Go through an n × n grid. Print j where the cell is on a diagonal: the main diagonal is j == i and the other one is j == n − i + 1. Every other cell is a space. With an odd n the two lines meet in a single middle cell.
This program is part of the Number Patterns collection.
Table of contents8 sections
Number X Pattern output
Output (n = 5):
1 5 2 4 3 2 4 1 5

How the number x pattern works
On the main diagonal the row and column numbers are equal. On the other diagonal they always add up to n + 1, so j = n − i + 1. Testing both conditions with || draws the two lines at once. Using the column number as the value keeps the digits symmetrical: the left arm reads 1, 2 going in and the right arm 5, 4.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
npositions in the row. Print numbers (valuej) wherej = i or j = n − i + 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- The main diagonal is
j == i; the other isj == n − i + 1. - Cells on the second diagonal always have
i + j = n + 1. - An odd
ngives a single centre cell; an evenngives two cells side by side in the middle rows. - The same test with a star instead of
jgives the X (cross) pattern.
Number X Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1)
printf("%d", j);
else
printf(" ");
}
printf("\n");
}
return 0;
}Number X Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1)
cout << j;
else
cout << " ";
}
cout << endl;
}
return 0;
}Number X Pattern in Java
public class NumberX {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
if (j == i || j == n - i + 1)
System.out.print(j);
else
System.out.print(" ");
}
System.out.println();
}
}
}Number X Pattern in Python
n = 5
for i in range(1, n + 1):
for j in range(1, n + 1):
if j == i or j == n - i + 1:
print(j, end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(n²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
What happens with an even n?
The two diagonals never share a cell, so the middle two rows each have two numbers next to each other, for example 23 and 23 for n = 4. The X still forms, it just has no single centre.
Can I print the row number instead?
Yes, print i. The X then reads 1 1, 2 2, 3, 4 4, 5 5, which shows the rows instead of the columns.
How do I add a square border around the X?
Add i == 1 || i == n || j == 1 || j == n to the condition. The pattern becomes a framed X.






