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
X Inside a Box Pattern output
Output (n = 7):
####### ## ## # # # # # # # # # # # ## ## #######

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
npositions in the row. Print#symbols wherei = 1 or i = n or j = 1 or j = n or j = i or j = n − i + 1; print spaces everywhere else. - 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
ngives 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.






