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

Slash Wave Pattern (/\/\)

The slash wave pattern draws a row of mountain peaks with forward slashes and backslashes. It only needs two lines, because each slash already climbs or falls by itself.

Quick answer: Print " /\ " n times on the first line and "/ \" n times on the second. Each four-character block is one peak; side by side, the blocks join into a continuous wave. Four blocks give four peaks and a line sixteen characters long.

This program is part of the Wave Patterns collection.

Table of contents8 sections
  1. Slash Wave Pattern output
  2. How the slash wave pattern works
  3. Slash Wave Pattern in C
  4. Slash Wave Pattern in C++
  5. Slash Wave Pattern in Java
  6. Slash Wave Pattern in Python
  7. Time and space complexity
  8. Frequently asked questions

Slash Wave Pattern output

Output (4 waves):

 /\  /\  /\  /\
/  \/  \/  \/  \
Slash Wave Pattern layout diagram, row by row (4 waves)
How each row of the slash wave pattern is built (4 waves). Blue cells are printed characters; grey dots are spaces.

How the slash wave pattern works

Each peak is a tiny 4 × 2 drawing: the top line has the tip, /\, with a space either side, and the bottom line has the two feet, / and \, with two spaces between them. When the blocks are placed next to each other, the backslash of one peak meets the slash of the next, which makes the valleys. In code, a backslash has to be written as \\ inside a string.

  1. Print the block " /\ " n times, then a new line.
  2. Print the block "/ \" n times, then a new line.

Key takeaways

  • Each peak is a 4 × 2 block of characters.
  • Neighbouring blocks share their valleys.
  • A backslash is written \\ inside a string literal.
  • Java’s repeat and Python’s * print a block n times without a loop.

Slash Wave Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;                                  /* number of waves */

    for (int k = 0; k < n; k++)
        printf(" /\\ ");
    printf("\n");
    for (int k = 0; k < n; k++)
        printf("/  \\");
    printf("\n");
    return 0;
}

Slash Wave Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;                                  // number of waves

    for (int k = 0; k < n; k++)
        cout << " /\\ ";
    cout << endl;
    for (int k = 0; k < n; k++)
        cout << "/  \\";
    cout << endl;
    return 0;
}

Slash Wave Pattern in Java

public class SlashWave {
    public static void main(String[] args) {
        int n = 4;                                  // number of waves

        System.out.println(" /\\ ".repeat(n));
        System.out.println("/  \\".repeat(n));
    }
}

Slash Wave Pattern in Python

n = 4                                       # number of waves

print(" /\\ " * n)
print("/  \\" * n)

Time and space complexity

The program prints two lines of 4n characters, so it runs in O(n) time with O(1) extra memory.

Frequently asked questions

How do I make taller peaks?

Use more lines per block. A peak of height 3 needs blocks like " /\ ", " / \ " and "/ \", which is the top half of the slash diamond.

Why does the code show four backslashes in some places?

Each pair stands for one backslash in the output. The C and C++ strings need \\ for every backslash you want to see.

Can I flip the waves upside down?

Yes. Print the feet line first with the slashes swapped, "\ /", and then " \/ ". The peaks then hang down like valleys.

How is it different from the mountain wave?

The mountain wave fills each peak with stars. The slash wave only draws the outline, so it looks lighter.

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 11 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.