New: 213 pattern programs with tested code in C, C++, Java and Python Explore patterns

Number X Pattern

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
  1. Output
  2. How the number x pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Number X Pattern output

Output (n = 5):

1   5
 2 4
  3
 2 4
1   5
Number X Pattern layout diagram, row by row (n = 5)
How each row of the number x pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through n positions in the row. Print numbers (value j) where j = i or j = n − i + 1; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • The main diagonal is j == i; the other is j == n − i + 1.
  • Cells on the second diagonal always have i + j = n + 1.
  • An odd n gives a single centre cell; an even n gives two cells side by side in the middle rows.
  • The same test with a star instead of j gives 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 20 of 24 in Number Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.