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

Slash Diamond Pattern (/\ \/)

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
  1. Slash Diamond Pattern output
  2. How the slash diamond pattern works
  3. Slash Diamond Pattern in C
  4. Slash Diamond Pattern in C++
  5. Slash Diamond Pattern in Java
  6. Slash Diamond Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Slash Diamond Pattern output

Output (n = 3):

  /\
 /  \
/    \
\    /
 \  /
  \/
Slash Diamond Pattern layout diagram, row by row (n = 3)
How each row of the slash diamond pattern is built (n = 3). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass (top half).
  2. Print n − i spaces.
  3. Print 1 / symbols.
  4. Print 2 × i − 2 spaces.
  5. Print 1 \\ symbols.
  6. Move to the next line.
  7. Run the outer loop for i from n down to 1, one row per pass (bottom half).
  8. Print n − i spaces.
  9. Print 1 \\ symbols.
  10. Print 2 × i − 2 spaces.
  11. Print 1 / symbols.
  12. Move to the next line.

Key takeaways

  • Each slash draws a diagonal step by itself.
  • The gap between the slashes is 2i − 2 spaces.
  • 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.

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 14 of 17 in Symbol 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.