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++
#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; } |
Java
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. |
Python3
# 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. |
C#
// 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. |
PHP
<?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 ?> |
Javascript
<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 :
/\ /\ /\ /\ / \ / \ / \ / \ / \ / \ / \ / \ / \/ \/ \/ \
Time Complexity: O(h2*l), Here h is the height of the wave and l is the length of the wave.
Auxiliary Space: O(1), As constant extra space is used.
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++
// 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; } |
Java
// 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. |
Python3
# 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. |
C#
// 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. |
PHP
<?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 ?> |
Javascript
<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
Time Complexity: O(h2*l), Here h is the height of the wave and l is the length of the wave.
Auxiliary Space: O(1), As constant extra space is used.
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++
#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; } |
Java
// 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. |
Python3
# 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. |
C#
// 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 |
PHP
<?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 ?> |
Javascript
<script> // Javascript Program to print // wave pattern // Function definition function 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 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 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
Time Complexity: O(h2*l), Here h is the height of the wave and l is the length of the wave.
Auxiliary Space: O(1), As constant extra space is used.