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

Diamond Chain Wave Pattern

The diamond chain wave puts two zig-zag waves on top of each other, one the mirror of the other, so they cross and form a chain of hollow diamonds. It looks like a rope or a lattice fence.

Quick answer: Each wave repeats every 2n βˆ’ 2 columns. On the upper rows the stars sit at offsets n βˆ’ i and its mirror; on the lower rows at i βˆ’ n and its mirror. Printing both mirrored offsets on every row draws the two waves at once, and where they meet they close into diamonds.

This program is part of the Wave Patterns collection.

Table of contents8 sections
  1. Output
  2. How the diamond chain wave 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 Chain Wave Pattern output

Output (5 rows, 4 diamonds):

  *   *   *   *
 * * * * * * * *
*   *   *   *   *
 * * * * * * * *
  *   *   *   *
Diamond Chain Wave Pattern layout diagram, row by row (5 rows, 4 diamonds)
How each row of the diamond chain wave pattern is built (5 rows, 4 diamonds). Blue cells are printed characters; grey dots are spaces.

How the diamond chain wave pattern works

Picture a single zig-zag of height n. Flip it upside down and lay it over the original: the two lines cross at the middle row and meet again at the top and bottom, so each repeat becomes a diamond. In code, every row has two star positions per repeat, at offset k and at 2n βˆ’ 2 βˆ’ k, where k is the distance from the middle row. The remainder by 2n βˆ’ 2 repeats the diamonds along the line.

  1. Run the outer loop for i from 1 to n, one row per pass (upper half).
  2. Go through 8 Γ— n βˆ’ 7 positions in the row. Print stars where (j βˆ’ 1) % (2 Γ— n βˆ’ 2) = n βˆ’ i or (j βˆ’ 1) % (2 Γ— n βˆ’ 2) = (n + i βˆ’ 2) % (2 Γ— n βˆ’ 2); print spaces everywhere else.
  3. Move to the next line.
  4. Run the outer loop for i from n + 1 to 2 Γ— n βˆ’ 1, one row per pass (lower half).
  5. Go through 8 Γ— n βˆ’ 7 positions in the row. Print stars where (j βˆ’ 1) % (2 Γ— n βˆ’ 2) = i βˆ’ n or (j βˆ’ 1) % (2 Γ— n βˆ’ 2) = (3 Γ— n βˆ’ 2 βˆ’ i) % (2 Γ— n βˆ’ 2); print spaces everywhere else.
  6. Move to the next line.

Key takeaways

  • The chain is two mirrored zig-zags drawn together.
  • Each diamond is 2n βˆ’ 2 columns wide.
  • Every row has two stars per diamond, at mirrored offsets.
  • The middle row is where the two waves cross.

Diamond Chain Wave Pattern in C

#include <stdio.h>

int main(void) {
    int n = 3;

    // upper half
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 8 * n - 7; j++) {
            if ((j - 1) % (2 * n - 2) == n - i || (j - 1) % (2 * n - 2) == (n + i - 2) % (2 * n - 2))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    // lower half
    for (int i = n + 1; i <= 2 * n - 1; i++) {
        for (int j = 1; j <= 8 * n - 7; j++) {
            if ((j - 1) % (2 * n - 2) == i - n || (j - 1) % (2 * n - 2) == (3 * n - 2 - i) % (2 * n - 2))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Diamond Chain Wave Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 3;

    // upper half
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= 8 * n - 7; j++) {
            if ((j - 1) % (2 * n - 2) == n - i || (j - 1) % (2 * n - 2) == (n + i - 2) % (2 * n - 2))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }

    // lower half
    for (int i = n + 1; i <= 2 * n - 1; i++) {
        for (int j = 1; j <= 8 * n - 7; j++) {
            if ((j - 1) % (2 * n - 2) == i - n || (j - 1) % (2 * n - 2) == (3 * n - 2 - i) % (2 * n - 2))
                cout << "*";
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Diamond Chain Wave Pattern in Java

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

        // upper half
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 8 * n - 7; j++) {
                if ((j - 1) % (2 * n - 2) == n - i || (j - 1) % (2 * n - 2) == (n + i - 2) % (2 * n - 2))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }

        // lower half
        for (int i = n + 1; i <= 2 * n - 1; i++) {
            for (int j = 1; j <= 8 * n - 7; j++) {
                if ((j - 1) % (2 * n - 2) == i - n || (j - 1) % (2 * n - 2) == (3 * n - 2 - i) % (2 * n - 2))
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Diamond Chain Wave Pattern in Python

n = 3

# upper half
for i in range(1, n + 1):
    for j in range(1, 8 * n - 7 + 1):
        if (j - 1) % (2 * n - 2) == n - i or (j - 1) % (2 * n - 2) == (n + i - 2) % (2 * n - 2):
            print("*", end="")
        else:
            print(" ", end="")
    print()

# lower half
for i in range(n + 1, 2 * n):
    for j in range(1, 8 * n - 7 + 1):
        if (j - 1) % (2 * n - 2) == i - n or (j - 1) % (2 * n - 2) == (3 * n - 2 - i) % (2 * n - 2):
            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 add more diamonds?

Widen the grid. Each diamond needs 2n βˆ’ 2 columns, plus one column to close the last one, so d diamonds need d(2n βˆ’ 2) + 1 columns.

How is it different from the hollow diamond?

The hollow diamond is a single shape. Here the diamonds share their corners and repeat along the row.

Why are there two parts in the code?

The upper and lower halves measure the distance to the middle row differently, n βˆ’ i and i βˆ’ n. Splitting them avoids an absolute-value function.

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 12 of 12 in Wave 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.