Wave Patterns - GeeksforGeeks

29 Oct, 2021
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++
Java
Python3
C#
PHP
Javascript
#include <iostream>using namespace std; // Function definitionvoid 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 codeint 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;}
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.
# Function definitiondef 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 wavewave_height = 4 # change value to decrease or increase# the length of wavewave_length = 4 pattern(wave_height, wave_length); # This code is contributed by Nikita Tiwari.
// C# code for Wave Patternsusing 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.
<?php// PHP implementation to// print wave patterns // Function definitionfunction 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?>
<script>     // Function definition    function pattern( wave_height , wave_length)    {        let 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++)                document.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--)                    document.write("  ");                 for (k = 1; k <= e; k++)                {                    if (k == 1)                        document.write("/");                    else if (k == e)                        document.write("\\");                    else                        document.write("  ");                }            }             // incrementing counters            // value by two            x = x + 2;            e = e + 2;             document.write("<br/>");        }    }     // Driver code          // change value to decrease or    // increase the height of wave    let wave_height = 4;         // change value to decrease or    // increase the length of wave    let wave_length = 4;     pattern(wave_height, wave_length); // This code is contributed by todaysgaurav</script>
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++
Java
Python3
C#
PHP
Javascript
// C++ code to print numbers// in wave form#include <iostream>using namespace std; // Function definitionvoid 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 codeint 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;}
// Java code to print numbers// in wave formimport 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.
# Python 3 code to print numbers# in wave form # Function definitiondef 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 wavewave_height = 4 # change value to decrease or# increase the length of wavewave_length = 4 pattern(wave_height, wave_length) # This code is contributed by Nikita Tiwari.
// C# code to print numbers// in wave formusing 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.
<?php// PHP implementation to print// numbers in wave form // Function definitionfunction 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?>
<script>// javascript code to print numbers// in wave form     // Function definition    function pattern(wave_height , wave_length) {        var i, j, k, e, n;        var 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++) {                document.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--)                    document.write(" ");                 for (k = 1; k <= e; k++) {                    if (k == 1)                        document.write("0" + n1 + " ");                    else if (k == e)                        document.write("0" + n2 + " ");                    else                        document.write(" ");                }            }             // incrementing counters            // value by two            x = x + 2;            e = e + 2;            n1 = wave_height - i;            n2 = wave_height + 1 + i;             document.write("<br/>");        }    }     // Driver code             // change value to decrease or        // increase the height of wave        var wave_height = 4;         // change value to decrease or        // increase the length of wave        var wave_length = 4;         pattern(wave_height, wave_length); // This code contributed by gauravrajput1</script>
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 
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++
Java
Python3
C#
PHP
Javascript
#include <iostream>using namespace std; // Function definitionvoid 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 codeint 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;}
// Java Program to print// wave patternimport 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.
# Python3 program to print# the wave pattern # Function definitiondef 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 wavewave_height = 4 # change value to decrease or# increase the length of wavewave_length = 4 pattern(wave_height, wave_length) # This code is contributed by Nikita Tiwari.
// C# Program to print// wave patternusing System; class GFG{// Function definitionstatic 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 codestatic 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
<?php// PHP implementation to print// Wave pattern using Alphabets // Function definitionfunction 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?>
<script> // Javascript Program to print// wave pattern // Function definitionfunction pattern(wave_height, wave_length){    var i, j, k, e, n, count, x;    e = 2;    x = 1;     var c1 = 'A'.charCodeAt(0) + wave_height - 1;    var c2 = 'A'.charCodeAt(0) + wave_height;     // For loop for height    // of wave    for(i = 1; i <= wave_height; i++)    {        for(j = wave_height; j <= wave_height + i; j++)        {            document.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--)                document.write(" ");                             for(k = 1; k <= e; k++)            {                if (k == 1)                    document.write(                        String.fromCharCode(c1) + " ");                else if (k == e)                    document.write(                        String.fromCharCode(c2) + " ");                else                    document.write(" ");            }                         c1 = c1 + wave_height * 2;            c2 = c2 + wave_height * 2;             // checking the limit            if (c1 > 'Z'.charCodeAt(0))                c1 = c1 - 26;                         if (c2 > 'Z'.charCodeAt(0))                c2 = c2 - 26;        }         // Incrementing counters        // value by two        x = x + 2;        e = e + 2;        c1 = 'A'.charCodeAt(0) + wave_height - i - 1;        c2 = 'A'.charCodeAt(0) + wave_height + i;                 document.write('<br>');    }} // Driver code // Change value to decrease or// increase the height of wavevar wave_height = 4; // Change value to decrease or// increase the length of wavevar wave_length = 4; pattern(wave_height, wave_length); // This code is contributed by Princi Singh </script>
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
 
Mithun Kumar
jit_t
todaysgaurav
GauravRajput1
princi singh
bunnyram19
khushboogoyal499
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Interfaces in Java
C++ Classes and Objects
Operator Overloading in C++
Constructors in C++
Copy Constructor in C++
Overriding in Java
Polymorphism in C++
C++ Data Types
Different ways of Reading a text file in Java
Types of Operating Systems