Wave Patterns

Given length and width, print the pattern in wave form using ‘/’ and ”.

Examples :

Input : wave_height = 4
        wave_length = 4
Output :
   /\      /\      /\      /\      
  /  \    /  \    /  \    /  \    
 /    \  /    \  /    \  /    \  
/      \/      \/      \/      \      

Input : wave_height = 2
        wave_length = 3
Output : 
 /\  /\  /\
/  \/  \/  \



C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
// Function definition
void pattern(int wave_height, int wave_length)
{
    int i, j, k, e, n, count, x;
    e = 2;
    x = 1;
  
    // for loop for height of wave
    for (i = 0; i < wave_height; i++) {
  
        for (j = wave_height; j <= wave_height + i; j++) 
            cout << " ";       
  
        // for loop for wave length
        for (count = 1; count <= wave_length; count++) {
  
            // checking for intermediate spaces
            for (n = (wave_height + wave_height - 2); n >= x; n--)
                cout << " ";
            for (k = 1; k <= e; k++) {
                if (k == 1)
                    cout << "/";
                else if (k == e)
                    cout << "\\";
                else
                    cout << " ";
            }
        }
  
        // incrementing counters value by two
        x = x + 2;
        e = e + 2;
        cout << endl;
    }
}
  
// Driver code
int main()
{
    // change value to decrease or increase
    // the height of wave
    int wave_height = 4;
  
    // change value to decrease or increase 
    // the length of wave
    int wave_length = 4;
  
    pattern(wave_height, wave_length);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
  
class GFG {
  
    // Function definition
    static void pattern(int wave_height,
                        int wave_length)
    {
        int i, j, k, e, n, count, x;
        e = 2;
        x = 1;
  
        // for loop for height
        // of wave
        for (i = 0; i < wave_height; i++)
        {
            for (j = wave_height; j <= wave_height + i; j++)
                System.out.print(" ");
  
            // for loop for wave
            // length
            for (count = 1; count <= wave_length; count++)
            {
                // checking for intermediate
                // spaces
                for (n = (wave_height + wave_height - 2); n >= x; n--)
                    System.out.print(" ");
              
                for (k = 1; k <= e; k++)
                {
                    if (k == 1)
                        System.out.print("/");
                    else if (k == e)
                        System.out.print("\\");
                    else
                        System.out.print(" ");
                }
            }
  
            // incrementing counters 
            // value by two
            x = x + 2;
            e = e + 2;
              
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        // change value to decrease or
        // increase the height of wave
        int wave_height = 4;
  
        // change value to decrease or
        // increase the length of wave
        int wave_length = 4;
  
        pattern(wave_height, wave_length);
    }
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Function definition
def pattern(wave_height, wave_length) :
  
    e = 2
    x = 1
  
    # for loop for height
    # of wave
    for i in range(0, wave_height) :
      
        for j in range(wave_height, wave_height + i+1) :
            print(" ", end ="")     
  
        # for loop for wave
        # length
        for count in range(1, wave_length + 1) :
          
            # checking for intermediate spaces
            for n in range((wave_height + wave_height - 2), x-1, -1) :
                print(" ", end ="")
          
            for k in range(1, e + 1) :
          
                if (k == 1) :
                    print("/", end ="")
                elif (k == e) :
                    print("\\", end ="")
                else :
                    print(" ", end ="")
              
        # incrementing counters 
        # value by two
        x = x + 2
        e = e + 2
          
        print("")
  
# Driver code
  
# Change value to decrease or increase
# the height of wave
wave_height = 4
  
# change value to decrease or increase 
# the length of wave
wave_length = 4
  
pattern(wave_height, wave_length);
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code for Wave Patterns
using System;
  
class GFG {
  
// Function definition
    static void pattern(int wave_height,
                        int wave_length)
    {
        int i, j, k, e, n, count, x;
        e = 2;
        x = 1;
  
        // for loop for height
        // of wave
        for (i = 0; i < wave_height; i++)
        {
            for (j = wave_height; j <= wave_height + i; j++)
                Console.Write(" ");
  
            // for loop for wave
            // length
            for (count = 1; count <= wave_length; count++)
            {
                // checking for intermediate
                // spaces
                for (n = (wave_height + wave_height - 2); n >= x; n--)
                    Console.Write(" ");
              
                for (k = 1; k <= e; k++)
                {
                    if (k == 1)
                    Console.Write("/");
                    else if (k == e)
                    Console.Write("\\");
                    else
                    Console.Write(" ");
                }
            }
  
            // incrementing counters 
            // value by two
            x = x + 2;
            e = e + 2;
              
        Console.WriteLine();
        }
    }
  
    // Driver code
    public static void Main()
    {
        // change value to decrease or
        // increase the height of wave
        int wave_height = 4;
  
        // change value to decrease or
        // increase the length of wave
        int wave_length = 4;
  
        pattern(wave_height, wave_length);
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to 
// print wave patterns
  
// Function definition
function pattern($wave_height,$wave_length)
{
    $e = 2;
    $x = 1;
  
    // for loop for height of wave
    for ($i = 0; $i < $wave_height; $i++) 
    {
        for ($j = $wave_height
             $j <= $wave_height + $i
             $j++) 
            echo " "
  
        // for loop for wave length
        for ($count = 1; 
             $count <= $wave_length
             $count++) 
             {
  
            // checking for intermediate 
            // spaces
            for ($n = ($wave_height
                       $wave_height - 2); 
                       $n >= $x; $n--)
                echo " ";
              
            for ($k = 1; $k <= $e; $k++) 
            {
                if ($k == 1)
                    echo "/";
                else if ($k == $e)
                    echo "\\";
                else
                    echo " ";
            }
        }
  
        // incrementing counters value by two
        $x = $x + 2;
        $e = $e + 2;
        echo "\n";
    }
}
  
// Driver code
$wave_height = 4;
$wave_length = 4;
pattern($wave_height, $wave_length);
  
// This code is contributed by Mithun Kumar
?>

chevron_right



Output :

   /\      /\      /\      /\      
  /  \    /  \    /  \    /  \    
 /    \  /    \  /    \  /    \  
/      \/      \/      \/      \
     

To print numbers in wave form.

Examples :

Input : wave_height = 4
        wave_length = 4
Output :

   04 05       04 05       04 05       04 05
  03   06     03   06     03   06     03   06
 02     07   02     07   02     07   02     07
01       08 01       08 01       08 01       08 

Input : wave_height = 2
        wave_length = 2  
Output : 
  02     02
01  03 01  03

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code to print numbers
// in wave form
#include <iostream>
using namespace std;
  
// Function definition
void pattern(int wave_height, int wave_length)
{
    int i, j, k, e, n, count, x, n1, n2;
    e = 2;
    x = 1;
    n1 = wave_height;
    n2 = wave_height + 1;
  
    // for loop for height of wave
    for (i = 1; i <= wave_height; i++) {
        for (j = wave_height; j <= wave_height + i; j++) {
            cout << " ";
        }
  
        // for loop for wave length
        for (count = 1; count <= wave_length; count++) {
  
            // checking for intermediate spaces
            for (n = (wave_height + wave_height - 2); n >= x; n--)
                cout << " ";
            for (k = 1; k <= e; k++) {
                if (k == 1)
                    cout << "0" << n1 << " ";
                else if (k == e)
                    cout << "0" << n2 << " ";
                else
                    cout << " ";
            }
        }
  
        // incrementing counters value by two
        x = x + 2;
        e = e + 2;
        n1 = wave_height - i;
        n2 = wave_height + 1 + i;
        cout << endl;
    }
}
  
// Driver code
int main()
{
    // change value to decrease or
    // increase the height of wave
    int wave_height = 4;
  
    // change value to decrease or
    // increase the length of wave
    int wave_length = 4;
  
    pattern(wave_height, wave_length);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java code to print numbers
// in wave form
import java.io.*;
  
class GFG {
  
    // Function definition
    static void pattern(int wave_height,
                        int wave_length)
    {
        int i, j, k, e, n;
        int count, x, n1, n2;
          
        e = 2;
        x = 1;
        n1 = wave_height;
        n2 = wave_height + 1;
  
        // for loop for height 
        // of wave
        for (i = 1; i <= wave_height; i++)
        {
            for (j = wave_height; j <= wave_height + i; j++)
            {
                System.out.print(" ");
            }
  
            // for loop for wave
            // length
            for (count = 1; count <= wave_length; count++)
            {
  
                // checking for intermediate
                // spaces
                for (n = (wave_height + wave_height - 2); n >= x; n--)
                    System.out.print(" ");
              
                for (k = 1; k <= e; k++) 
                {
                    if (k == 1)
                        System.out.print("0" + n1 + " ");
                    else if (k == e)
                        System.out.print("0" + n2 + " ");
                    else
                        System.out.print(" ");
                }
            }
  
            // incrementing counters 
            // value by two
            x = x + 2;
            e = e + 2;
            n1 = wave_height - i;
            n2 = wave_height + 1 + i;
              
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        // change value to decrease or
        // increase the height of wave
        int wave_height = 4;
  
        // change value to decrease or
        // increase the length of wave
        int wave_length = 4;
  
        pattern(wave_height, wave_length);
    }
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to print numbers
# in wave form
  
# Function definition
def pattern( wave_height, wave_length) :
  
    e = 2
    x = 1
    n1 = wave_height
    n2 = wave_height + 1
  
    # for loop for height
    # of wave
    for i in range(1, wave_height + 1) :
      
        for j in range( wave_height, wave_height + i + 1) :
            print( " ", end ="")
          
          
        # for loop for wave
        # length
        for count in range(1, wave_length + 1) :
          
            # checking for intermediate
            # spaces
            for n in range((wave_height + wave_height - 2), x - 1, -1) :
                print( " ", end ="")
              
            for k in range(1, e + 1) :
              
                if (k == 1) :
                    print("0", n1, " ", end ="")
                elif (k == e) :
                    print("0", n2, " ", end ="")
                else :
                    print(" ", end ="")         
                      
        # incrementing counters value
        # by two
        x = x + 2
        e = e + 2
        n1 = wave_height - i
        n2 = wave_height + 1 + i
          
        print()
          
# Driver code
  
# change value to decrease or
# increase the height of wave
wave_height = 4
  
# change value to decrease or
# increase the length of wave
wave_length = 4
  
pattern(wave_height, wave_length)
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# code to print numbers
// in wave form
using System;
  
class GFG
{
      
    // Function definition
    static void pattern(int wave_height,
                        int wave_length)
    {
        int i, j, k, e, n;
        int count, x, n1, n2;
          
        e = 2;
        x = 1;
        n1 = wave_height;
        n2 = wave_height + 1;
  
        // for loop for 
        // height of wave
        for (i = 1; i <= wave_height; i++)
        {
            for (j = wave_height; 
                 j <= wave_height + i; j++)
            {
                Console.Write(" ");
            }
  
            // for loop for 
            // wave length
            for (count = 1; 
                 count <= wave_length; count++)
            {
  
                // checking for intermediate
                // spaces
                for (n = (wave_height + 
                          wave_height - 2);
                     n >= x; n--)
                    Console.Write(" ");
              
                for (k = 1; k <= e; k++) 
                {
                    if (k == 1)
                        Console.Write("0" + n1 + " ");
                    else if (k == e)
                        Console.Write("0" + n2 + " ");
                    else
                        Console.Write(" ");
                }
            }
  
            // incrementing counters 
            // value by two
            x = x + 2;
            e = e + 2;
            n1 = wave_height - i;
            n2 = wave_height + 1 + i;
              
            Console.WriteLine();
        }
    }
  
    // Driver code
    static public void Main ()
    {
          
        // change value to decrease or
        // increase the height of wave
        int wave_height = 4;
  
        // change value to decrease or
        // increase the length of wave
        int wave_length = 4;
  
        pattern(wave_height, wave_length);
    }
}
  
// This code is contributed by ajit.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print
// numbers in wave form
  
// Function definition
function pattern($wave_height
                 $wave_length)
{
    $e = 2;
    $x = 1;
    $n1 = $wave_height;
    $n2 = $wave_height + 1;
  
    // for loop for height of wave
    for ($i = 1; $i <= $wave_height; $i++)
    {
        for ($j = $wave_height;
             $j <= $wave_height + $i; $j++) 
        {
            echo " ";
        }
  
        // for loop for wave length
        for ($count = 1; 
             $count <= $wave_length
             $count++) 
        {
  
            // checking for intermediate 
            // spaces
            for ($n = ($wave_height
                       $wave_height - 2); 
                       $n >= $x; $n--)
                         
                echo " ";
                  
            for ($k = 1; $k <= $e; $k++) 
            {
                if ($k == 1)
                    echo "0".$n1." ";
                else if ($k == $e)
                    echo "0".$n2." ";
                else
                    echo " ";
            }
        }
  
        // incrementing counters value 
        // by two
        $x = $x + 2;
        $e = $e + 2;
        $n1 = $wave_height - $i;
        $n2 = $wave_height + 1 + $i;
        echo "\n";
    }
}
  
// Driver code
$wave_height = 4;
$wave_length = 4;
pattern($wave_height, $wave_length);
  
// This code is contributed by Mithun Kumar
?>

chevron_right



Output :


   04 05       04 05       04 05       04 05
  03   06     03   06     03   06     03   06
 02     07   02     07   02     07   02     07
01       08 01       08 01       08 01       08 

C++ program to print wave pattern using letters .

Examples :

Input : wave_height = 4
        wave_length = 4
Output :


   D E       L M       T U       B C      
  C   F     K   N     S   V     A   D 
 B     G   J     O   R     W   Z     E  
A       H I       P Q       X Y       F

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

#include <iostream>
using namespace std;
  
// Function definition
void pattern(int wave_height, int wave_length)
{
    int i, j, k, e, n, count, x;
    e = 2;
    x = 1;
  
    int c1 = 'A' + wave_height - 1;
    int c2 = 'A' + wave_height;
  
    // for loop for height of wave
    for (i = 1; i <= wave_height; i++) {
        for (j = wave_height; j <= wave_height + i; j++) {
            cout << " ";
        }
  
        // for loop for wave length
        for (count = 1; count <= wave_length; count++) {
  
            // checking for intermediate spaces
            for (n = (wave_height + wave_height - 2); n >= x; n--)
                cout << " ";
            for (k = 1; k <= e; k++) {
                if (k == 1)
                    cout << (char)c1 << " ";
                else if (k == e)
                    cout << (char)c2 << " ";
                else
                    cout << " ";
            }
            c1 = c1 + wave_height * 2;
            c2 = c2 + wave_height * 2;
  
            // checking the limit
            if (c1 > 'Z')
                c1 = c1 - 26;
            if (c2 > 'Z')
                c2 = c2 - 26;
        }
  
        // incrementing counters value by two
        x = x + 2;
        e = e + 2;
        c1 = 'A' + wave_height - i - 1;
        c2 = 'A' + wave_height + i;
        cout << endl;
    }
}
  
// Driver code
int main()
{
    // change value to decrease or increase
    // the height of wave
    int wave_height = 4;
  
    // change value to decrease or increase 
    // the length of wave
    int wave_length = 4;
  
    pattern(wave_height, wave_length);
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java Program to print 
// wave pattern
import java.io.*;
  
class GFG {
  
    // Function definition
    static void pattern(int wave_height,
                        int wave_length)
    {
        int i, j, k, e, n, count, x;
        e = 2;
        x = 1;
  
        int c1 = 'A' + wave_height - 1;
        int c2 = 'A' + wave_height;
  
        // for loop for height
        // of wave
        for (i = 1; i <= wave_height; i++)
        {
            for (j = wave_height; j <= wave_height + i; j++) 
            {
                System.out.print(" ");
            }
  
            // for loop for wave
            // length
            for (count = 1; count <= wave_length; count++)
            {
                // checking for intermediate
                // spaces
                for (n = (wave_height + wave_height - 2); n >= x; n--)
                    System.out.print(" ");
                      
                for (k = 1; k <= e; k++)
                {
                    if (k == 1)
                        System.out.print((char)c1 + " ");
                    else if (k == e)
                        System.out.print((char)c2 + " ");
                    else
                        System.out.print(" ");
                }
                  
                c1 = c1 + wave_height * 2;
                c2 = c2 + wave_height * 2;
  
                // checking the limit
                if (c1 > 'Z')
                    c1 = c1 - 26;
                  
                if (c2 > 'Z')
                    c2 = c2 - 26;
            }
  
            // incrementing counters
            // value by two
            x = x + 2;
            e = e + 2;
            c1 = 'A' + wave_height - i - 1;
            c2 = 'A' + wave_height + i;
              
            System.out.println();
        }
    }
  
    // Driver code
    public static void main(String args[])
    {
        // change value to decrease or
        // increase the height of wave
        int wave_height = 4;
  
        // change value to decrease or
        // increase the length of wave
        int wave_length = 4;
  
        pattern(wave_height, wave_length);
    }
}
  
// This code is contributed by Nikita Tiwari.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python3 program to print
# the wave pattern
  
# Function definition
def pattern(wave_height, wave_length) :
    e = 2
    x = 1
  
    c1 = ord('A') + wave_height - 1
    c2 = ord('A') + wave_height
  
    # for loop for height
    # of wave
    for i in range(1, wave_height + 1) :
      
        for j in range(wave_height, wave_height + i + 1 ):
            print( " ", end = "")
          
          
        # for loop for wave
        # length
        for count in range(1, wave_length + 1) :
          
            # checking for intermediate
            # spaces
            for n in range((wave_height + wave_height - 2), x - 1, -1) :
                print(" ", end = "")
              
            for k in range(1, e + 1) :
              
                if (k == 1) :
                    print((chr)(c1), " ", end = "")
                elif (k == e) :
                    print((chr)(c2), " ", end = "")
                else :
                    print(" ", end = "")
              
            c1 = c1 + wave_height * 2
            c2 = c2 + wave_height * 2
  
            # checking the limit
            if (c1 > ord('Z')) :
                c1 = c1 - 26
                  
            if (c2 > ord('Z')) :
                c2 = c2 - 26
          
        # incrementing counters 
        # value by two
        x = x + 2;
        e = e + 2
        c1 = ord('A') + wave_height - i - 1
        c2 = ord('A') + wave_height + i
  
        print()
      
# Driver code
  
# change value to decrease or
# increase the height of wave
wave_height = 4
  
# change value to decrease or 
# increase the length of wave
wave_length = 4
  
pattern(wave_height, wave_length)
  
# This code is contributed by Nikita Tiwari.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# Program to print 
// wave pattern
using System;
  
class GFG
{
// Function definition
static void pattern(int wave_height,
                    int wave_length)
{
    int i, j, k, e, n, count, x;
    e = 2;
    x = 1;
  
    int c1 = 'A' + wave_height - 1;
    int c2 = 'A' + wave_height;
  
    // for loop for height
    // of wave
    for (i = 1; i <= wave_height; i++)
    {
        for (j = wave_height; 
             j <= wave_height + i; j++) 
        {
            Console.Write(" ");
        }
  
        // for loop for wave
        // length
        for (count = 1; 
             count <= wave_length; count++)
        {
            // checking for intermediate
            // spaces
            for (n = (wave_height +
                      wave_height - 2);
                 n >= x; n--)
                Console.Write(" ");
                  
            for (k = 1; k <= e; k++)
            {
                if (k == 1)
                    Console.Write((char)c1 + " ");
                else if (k == e)
                    Console.Write((char)c2 + " ");
                else
                    Console.Write(" ");
            }
              
            c1 = c1 + wave_height * 2;
            c2 = c2 + wave_height * 2;
  
            // checking the limit
            if (c1 > 'Z')
                c1 = c1 - 26;
              
            if (c2 > 'Z')
                c2 = c2 - 26;
        }
  
        // incrementing counters
        // value by two
        x = x + 2;
        e = e + 2;
        c1 = 'A' + wave_height - i - 1;
        c2 = 'A' + wave_height + i;
          
        Console.WriteLine();
    }
}
  
// Driver code
static public void Main ()
{
    // change value to decrease or
    // increase the height of wave
    int wave_height = 4;
  
    // change value to decrease or
    // increase the length of wave
    int wave_length = 4;
  
    pattern(wave_height, wave_length);
}
}
  
// This code is contributed by ajit

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP implementation to print
// Wave pattern using Alphabets
  
// Function definition
function pattern($wave_height
                 $wave_length)
{
    $e = 2;
    $x = 1;
      
    //ASCII of A is 65
    $c1 = 65 + $wave_height - 1;
    $c2 = 65 + $wave_height;
  
    // for loop for height of wave
    for ($i = 1; $i <= $wave_height
                               $i++) 
    {
        for ($j = $wave_height
             $j <= $wave_height + $i
             $j++) 
        {
            echo " ";
        }
  
        // for loop for wave length
        for ($count = 1; 
             $count <= $wave_length
             $count++) 
        {
  
            // checking for intermediate 
            // spaces
            for ($n = ($wave_height
                       $wave_height - 2); 
                       $n >= $x; $n--)
                         
                echo " ";
                  
            for ($k = 1; $k <= $e; $k++) 
            {
                if ($k == 1)
                    echo chr($c1)." ";
                else if ($k == $e)
                    echo chr($c2)." ";
                else
                    echo " ";
            }
            $c1 = $c1 + $wave_height * 2;
            $c2 = $c2 + $wave_height * 2;
  
            // checking the limit
            if ($c1 > 90)
                $c1 = $c1 - 26;
            if ($c2 > 90)
                $c2 = $c2 - 26;
        }
  
        // incrementing counters value 
        // by two
        $x = $x + 2;
        $e = $e + 2;
        $c1 = 65 + $wave_height - $i - 1;
        $c2 = 65 + $wave_height + $i;
        echo "\n";
    }
}
  
// Driver code
$wave_height = 4;
$wave_length = 4;
pattern($wave_height, $wave_length);
  
// This code is contributed by Mithun Kumar
?>

chevron_right



Output :


   D E       L M       T U       B C      
  C   F     K   N     S   V     A   D 
 B     G   J     O   R     W   Z     E  
A       H I       P Q       X Y       F



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



Improved By : Mithun Kumar, jit_t