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

X Inside a Box Pattern (#)

The X inside a box pattern draws a square frame of hash signs with both diagonals inside it, like an envelope seen from the back or a crossed-out box.

Quick answer: Loop over an n × n grid. Print # on the border (first or last row or column) and on the two diagonals, j == i and j == n − i + 1. Everything else is a space.

This program is part of the Symbol Patterns collection.

Table of contents8 sections
  1. X Inside a Box Pattern output
  2. How the x inside a box pattern works
  3. X Inside a Box Pattern in C
  4. X Inside a Box Pattern in C++
  5. X Inside a Box Pattern in Java
  6. X Inside a Box Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

X Inside a Box Pattern output

Output (n = 7):

#######
##   ##
# # # #
#  #  #
# # # #
##   ##
#######
X Inside a Box Pattern layout diagram, row by row (n = 7)
How each row of the x inside a box pattern is built (n = 7). Blue cells are printed characters; grey dots are spaces.

How the x inside a box pattern works

It joins two patterns you may already know: the hollow square and the X (cross). Their conditions are simply combined with ||. The diagonals start and end in the corners, so they meet the frame without any special case.

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

Key takeaways

  • The pattern is a hollow square plus an X.
  • Six simple tests, joined with ||, cover every #.
  • An odd n gives a single centre cell.
  • The site’s hollow square with diagonals is the star version.

X Inside a Box Pattern in C

#include <stdio.h>

int main(void) {
    int n = 7;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n || j == i || j == n - i + 1)
                printf("#");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

X Inside a Box Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 7;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            if (i == 1 || i == n || j == 1 || j == n || j == i || j == n - i + 1)
                cout << "#";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

X Inside a Box Pattern in Java

public class XInBox {
    public static void main(String[] args) {
        int n = 7;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                if (i == 1 || i == n || j == 1 || j == n || j == i || j == n - i + 1)
                    System.out.print("#");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

X Inside a Box Pattern in Python

n = 7

for i in range(1, n + 1):
    for j in range(1, n + 1):
        if i == 1 or i == n or j == 1 or j == n or j == i or j == n - i + 1:
            print("#", 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

How is it different from the hollow square with diagonals?

Only the character. The star version uses the same six tests.

What happens with an even n?

The two diagonals no longer share a centre cell. They pass next to each other in the middle two rows, so the centre shows a small block of four hashes instead of a single one.

How do I draw only one diagonal?

Remove j == n − i + 1 from the condition. The box then has a single slash from top left to bottom right.

How do I let the user choose the size?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

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 13 of 17 in Symbol 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.