The slash diamond pattern draws a diamond outline with forward and backslashes instead of stars, which gives it clean diagonal edges. The top half uses / on the left and \ on the right, and the bottom half swaps them.
Quick answer: Row i of the top half prints n − i spaces, a /, 2i − 2 spaces and a \. The bottom half runs i from n back to 1 with the slashes swapped. In C, C++ and Java the backslash is written "\\".
This program is part of the Symbol Patterns collection.
Table of contents8 sections
Slash Diamond Pattern output
Output (n = 3):
/\ / \ / \ \ / \ / \/

How the slash diamond pattern works
Slashes are already slanted, so each one draws a whole diagonal step on its own. The left edge moves one column left per row and the right edge one column right, which is why the gap between them grows by two. In the bottom half the edges move back in, so a backslash now leans the right way on the left and a slash on the right.
- Run the outer loop for
ifrom1ton, one row per pass (top half). - Print
n − ispaces. - Print
1/symbols. - Print
2 × i − 2spaces. - Print
1\\symbols. - Move to the next line.
- Run the outer loop for
ifromndown to1, one row per pass (bottom half). - Print
n − ispaces. - Print
1\\symbols. - Print
2 × i − 2spaces. - Print
1/symbols. - Move to the next line.
Key takeaways
- Each slash draws a diagonal step by itself.
- The gap between the slashes is
2i − 2spaces. - The bottom half swaps
/and\. - A backslash must be written as
\\inside a string in C, C++, Java and Python.
Slash Diamond Pattern in C
#include <stdio.h>
int main(void) {
int n = 3;
// top half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
printf(" ");
for (int j = 1; j <= 1; j++)
printf("/");
for (int j = 1; j <= 2 * i - 2; j++)
printf(" ");
for (int j = 1; j <= 1; j++)
printf("\\");
printf("\n");
}
// bottom half
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++)
printf(" ");
for (int j = 1; j <= 1; j++)
printf("\\");
for (int j = 1; j <= 2 * i - 2; j++)
printf(" ");
for (int j = 1; j <= 1; j++)
printf("/");
printf("\n");
}
return 0;
}Slash Diamond Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 3;
// top half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= 1; j++)
cout << "/";
for (int j = 1; j <= 2 * i - 2; j++)
cout << " ";
for (int j = 1; j <= 1; j++)
cout << "\\";
cout << endl;
}
// bottom half
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++)
cout << " ";
for (int j = 1; j <= 1; j++)
cout << "\\";
for (int j = 1; j <= 2 * i - 2; j++)
cout << " ";
for (int j = 1; j <= 1; j++)
cout << "/";
cout << endl;
}
return 0;
}Slash Diamond Pattern in Java
public class SlashDiamond {
public static void main(String[] args) {
int n = 3;
// top half
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
for (int j = 1; j <= 1; j++)
System.out.print("/");
for (int j = 1; j <= 2 * i - 2; j++)
System.out.print(" ");
for (int j = 1; j <= 1; j++)
System.out.print("\\");
System.out.println();
}
// bottom half
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n - i; j++)
System.out.print(" ");
for (int j = 1; j <= 1; j++)
System.out.print("\\");
for (int j = 1; j <= 2 * i - 2; j++)
System.out.print(" ");
for (int j = 1; j <= 1; j++)
System.out.print("/");
System.out.println();
}
}
}Slash Diamond Pattern in Python
n = 3
# top half
for i in range(1, n + 1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, 2):
print("/", end="")
for j in range(1, 2 * i - 2 + 1):
print(" ", end="")
for j in range(1, 2):
print("\\", end="")
print()
# bottom half
for i in range(n, 0, -1):
for j in range(1, n - i + 1):
print(" ", end="")
for j in range(1, 2):
print("\\", end="")
for j in range(1, 2 * i - 2 + 1):
print(" ", end="")
for j in range(1, 2):
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
Why is the backslash written twice in the code?
Inside a string, \ starts an escape sequence such as \n. Writing \\ tells the compiler you mean one real backslash.
How do I make a bigger diamond?
Raise n. Each half gets more rows and the widest row is 2n characters.
Can I join several diamonds in a row?
Yes: build each row as a string and repeat it, keeping the spaces on both sides so the diamonds touch at their widest points.






