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
Diamond Chain Wave Pattern output
Output (5 rows, 4 diamonds):
* * * * * * * * * * * * * * * * * * * * * * * * * * * * *

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.
- Run the outer loop for
ifrom1ton, one row per pass (upper half). - Go through
8 Γ n β 7positions 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. - Move to the next line.
- Run the outer loop for
ifromn + 1to2 Γ n β 1, one row per pass (lower half). - Go through
8 Γ n β 7positions 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. - Move to the next line.
Key takeaways
- The chain is two mirrored zig-zags drawn together.
- Each diamond is
2n β 2columns 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.






