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

Diamond Inside a Square Pattern

Read summary with AI tools

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
  1. Output
  2. How the diamond inside a square 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

Diamond Inside a Square Pattern output

Output (n = 5):

*********
*  * *  *
* *   * *
**     **
*       *
**     **
* *   * *
*  * *  *
*********
Diamond Inside a Square Pattern layout diagram, row by row (n = 5)
How each row of the diamond inside a square pattern is built (n = 5). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to 2 × n − 1, one row per pass.
  2. Go through 2 × n − 1 positions in the row. Print stars where 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 spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • A diagonal line is a constant sum or difference of i and j.
  • 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.

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.

Latest articles

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.