The diamond inside a square pattern draws a square frame and a hollow diamond whose four corners touch the middle of each side. One grid with one condition per cell is enough to print it.
Quick answer: Loop over a (2n − 1) × (2n − 1) grid. Print a star when the cell is on the border (first or last row or column) or on one of the four diagonal lines of the diamond: i + j = n + 1, j − i = n − 1, i − j = n − 1 or i + j = 3n − 1. Everything else is a space.
This program is part of the Tricky Patterns collection.
Table of contents8 sections
Diamond Inside a Square Pattern output
Output (n = 5):
********* * * * * * * * * ** ** * * ** ** * * * * * * * * *********

How the diamond inside a square pattern works
Each side of the diamond is a straight diagonal line, and a diagonal line on a grid is just a fixed sum or difference of the row and column numbers. The top-left side runs from the middle of the top edge to the middle of the left edge, where i + j is always n + 1. The other three sides work the same way with a difference or a larger sum. Add the four border tests and the whole picture is one condition.
- Run the outer loop for
ifrom1to2 × n − 1, one row per pass. - Go through
2 × n − 1positions in the row. Print stars wherei = 1 or i = 2 × n − 1 or j = 1 or j = 2 × n − 1 or i + j = n + 1 or j − i = n − 1 or i − j = n − 1 or i + j = 3 × n − 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- A diagonal line is a constant sum or difference of
iandj. - The diamond’s corners land on the middle of each side because the grid has an odd size,
2n − 1. - The border needs four simple tests: first row, last row, first column, last column.
- Drawing the grid on paper and labelling a few cells is the fastest way to find the four equations.
Diamond Inside a Square Pattern in C
#include <stdio.h>
int main(void) {
int n = 5;
for (int i = 1; i <= 2 * n - 1; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (i == 1 || i == 2 * n - 1 || j == 1 || j == 2 * n - 1 || i + j == n + 1 || j - i == n - 1 || i - j == n - 1 || i + j == 3 * n - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Diamond Inside a Square Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 5;
for (int i = 1; i <= 2 * n - 1; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (i == 1 || i == 2 * n - 1 || j == 1 || j == 2 * n - 1 || i + j == n + 1 || j - i == n - 1 || i - j == n - 1 || i + j == 3 * n - 1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Diamond Inside a Square Pattern in Java
public class DiamondInSquare {
public static void main(String[] args) {
int n = 5;
for (int i = 1; i <= 2 * n - 1; i++) {
for (int j = 1; j <= 2 * n - 1; j++) {
if (i == 1 || i == 2 * n - 1 || j == 1 || j == 2 * n - 1 || i + j == n + 1 || j - i == n - 1 || i - j == n - 1 || i + j == 3 * n - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Diamond Inside a Square Pattern in Python
n = 5
for i in range(1, 2 * n):
for j in range(1, 2 * n):
if i == 1 or i == 2 * n - 1 or j == 1 or j == 2 * n - 1 or i + j == n + 1 or j - i == n - 1 or i - j == n - 1 or i + j == 3 * n - 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 do I print only the diamond, without the frame?
Remove the four border tests. You are left with the four diagonal conditions, which print the hollow diamond on its own.
Why does the grid have 2n − 1 rows?
An odd size gives the square a single middle row and column, so each corner of the diamond touches exactly one cell in the middle of a side. With an even size the corners would fall between two cells.
Can I use abs() instead of four equations?
Yes: the diamond is where |i − n| + |j − n| = n − 1. In C that needs <stdlib.h> for abs. The four equations do the same job with plain arithmetic.











