Wave Patterns
Last Updated :
20 Feb, 2023
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;
void pattern( int wave_height, int wave_length)
{
int i, j, k, e, n, count, x;
e = 2;
x = 1;
for (i = 0; i < wave_height; i++) {
for (j = wave_height; j <= wave_height + i; j++)
cout << " " ;
for (count = 1; count <= wave_length; count++) {
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 << " " ;
}
}
x = x + 2;
e = e + 2;
cout << endl;
}
}
int main()
{
int wave_height = 4;
int wave_length = 4;
pattern(wave_height, wave_length);
return 0;
}
|
Java
import java.io.*;
class GFG {
static void pattern( int wave_height,
int wave_length)
{
int i, j, k, e, n, count, x;
e = 2 ;
x = 1 ;
for (i = 0 ; i < wave_height; i++)
{
for (j = wave_height; j <= wave_height + i; j++)
System.out.print( " " );
for (count = 1 ; count <= wave_length; count++)
{
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( " " );
}
}
x = x + 2 ;
e = e + 2 ;
System.out.println();
}
}
public static void main(String args[])
{
int wave_height = 4 ;
int wave_length = 4 ;
pattern(wave_height, wave_length);
}
}
|
Python3
def pattern(wave_height, wave_length) :
e = 2
x = 1
for i in range ( 0 , wave_height) :
for j in range (wave_height, wave_height + i + 1 ) :
print ( " " , end = "")
for count in range ( 1 , wave_length + 1 ) :
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 = "")
x = x + 2
e = e + 2
print ("")
wave_height = 4
wave_length = 4
pattern(wave_height, wave_length);
|
C#
using System;
class GFG {
static void pattern( int wave_height,
int wave_length)
{
int i, j, k, e, n, count, x;
e = 2;
x = 1;
for (i = 0; i < wave_height; i++)
{
for (j = wave_height; j <= wave_height + i; j++)
Console.Write( " " );
for (count = 1; count <= wave_length; count++)
{
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( " " );
}
}
x = x + 2;
e = e + 2;
Console.WriteLine();
}
}
public static void Main()
{
int wave_height = 4;
int wave_length = 4;
pattern(wave_height, wave_length);
}
}
|
PHP
<?php
function pattern( $wave_height , $wave_length )
{
$e = 2;
$x = 1;
for ( $i = 0; $i < $wave_height ; $i ++)
{
for ( $j = $wave_height ;
$j <= $wave_height + $i ;
$j ++)
echo " " ;
for ( $count = 1;
$count <= $wave_length ;
$count ++)
{
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 " " ;
}
}
$x = $x + 2;
$e = $e + 2;
echo "\n" ;
}
}
$wave_height = 4;
$wave_length = 4;
pattern( $wave_height , $wave_length );
?>
|
Javascript
<script>
function pattern( wave_height , wave_length)
{
let i, j, k, e, n, count, x;
e = 2;
x = 1;
for (i = 0; i < wave_height; i++)
{
for (j = wave_height; j <= wave_height + i; j++)
document.write( " " );
for (count = 1; count <= wave_length; count++)
{
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( " " );
}
}
x = x + 2;
e = e + 2;
document.write( "<br/>" );
}
}
let wave_height = 4;
let wave_length = 4;
pattern(wave_height, wave_length);
</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++
#include <iostream>
using namespace std;
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 (i = 1; i <= wave_height; i++) {
for (j = wave_height; j <= wave_height + i; j++) {
cout << " " ;
}
for (count = 1; count <= wave_length; count++) {
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 << " " ;
}
}
x = x + 2;
e = e + 2;
n1 = wave_height - i;
n2 = wave_height + 1 + i;
cout << endl;
}
}
int main()
{
int wave_height = 4;
int wave_length = 4;
pattern(wave_height, wave_length);
return 0;
}
|
Java
import java.io.*;
class GFG {
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 (i = 1 ; i <= wave_height; i++)
{
for (j = wave_height; j <= wave_height + i; j++)
{
System.out.print( " " );
}
for (count = 1 ; count <= wave_length; count++)
{
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( " " );
}
}
x = x + 2 ;
e = e + 2 ;
n1 = wave_height - i;
n2 = wave_height + 1 + i;
System.out.println();
}
}
public static void main(String args[])
{
int wave_height = 4 ;
int wave_length = 4 ;
pattern(wave_height, wave_length);
}
}
|
Python3
def pattern( wave_height, wave_length) :
e = 2
x = 1
n1 = wave_height
n2 = wave_height + 1
for i in range ( 1 , wave_height + 1 ) :
for j in range ( wave_height, wave_height + i + 1 ) :
print ( " " , end = "")
for count in range ( 1 , wave_length + 1 ) :
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 = "")
x = x + 2
e = e + 2
n1 = wave_height - i
n2 = wave_height + 1 + i
print ()
wave_height = 4
wave_length = 4
pattern(wave_height, wave_length)
|
C#
using System;
class GFG
{
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 (i = 1; i <= wave_height; i++)
{
for (j = wave_height;
j <= wave_height + i; j++)
{
Console.Write( " " );
}
for (count = 1;
count <= wave_length; count++)
{
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( " " );
}
}
x = x + 2;
e = e + 2;
n1 = wave_height - i;
n2 = wave_height + 1 + i;
Console.WriteLine();
}
}
static public void Main ()
{
int wave_height = 4;
int wave_length = 4;
pattern(wave_height, wave_length);
}
}
|
PHP
<?php
function pattern( $wave_height ,
$wave_length )
{
$e = 2;
$x = 1;
$n1 = $wave_height ;
$n2 = $wave_height + 1;
for ( $i = 1; $i <= $wave_height ; $i ++)
{
for ( $j = $wave_height ;
$j <= $wave_height + $i ; $j ++)
{
echo " " ;
}
for ( $count = 1;
$count <= $wave_length ;
$count ++)
{
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 " " ;
}
}
$x = $x + 2;
$e = $e + 2;
$n1 = $wave_height - $i ;
$n2 = $wave_height + 1 + $i ;
echo "\n" ;
}
}
$wave_height = 4;
$wave_length = 4;
pattern( $wave_height , $wave_length );
?>
|
Javascript
<script>
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 (i = 1; i <= wave_height; i++) {
for (j = wave_height; j <= wave_height + i; j++) {
document.write( " " );
}
for (count = 1; count <= wave_length; count++) {
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( " " );
}
}
x = x + 2;
e = e + 2;
n1 = wave_height - i;
n2 = wave_height + 1 + i;
document.write( "<br/>" );
}
}
var wave_height = 4;
var wave_length = 4;
pattern(wave_height, wave_length);
</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;
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 (i = 1; i <= wave_height; i++) {
for (j = wave_height; j <= wave_height + i; j++) {
cout << " " ;
}
for (count = 1; count <= wave_length; count++) {
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;
if (c1 > 'Z' )
c1 = c1 - 26;
if (c2 > 'Z' )
c2 = c2 - 26;
}
x = x + 2;
e = e + 2;
c1 = 'A' + wave_height - i - 1;
c2 = 'A' + wave_height + i;
cout << endl;
}
}
int main()
{
int wave_height = 4;
int wave_length = 4;
pattern(wave_height, wave_length);
return 0;
}
|
Java
import java.io.*;
class GFG {
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 (i = 1 ; i <= wave_height; i++)
{
for (j = wave_height; j <= wave_height + i; j++)
{
System.out.print( " " );
}
for (count = 1 ; count <= wave_length; count++)
{
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 ;
if (c1 > 'Z' )
c1 = c1 - 26 ;
if (c2 > 'Z' )
c2 = c2 - 26 ;
}
x = x + 2 ;
e = e + 2 ;
c1 = 'A' + wave_height - i - 1 ;
c2 = 'A' + wave_height + i;
System.out.println();
}
}
public static void main(String args[])
{
int wave_height = 4 ;
int wave_length = 4 ;
pattern(wave_height, wave_length);
}
}
|
Python3
def pattern(wave_height, wave_length) :
e = 2
x = 1
c1 = ord ( 'A' ) + wave_height - 1
c2 = ord ( 'A' ) + wave_height
for i in range ( 1 , wave_height + 1 ) :
for j in range (wave_height, wave_height + i + 1 ):
print ( " " , end = "")
for count in range ( 1 , wave_length + 1 ) :
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
if (c1 > ord ( 'Z' )) :
c1 = c1 - 26
if (c2 > ord ( 'Z' )) :
c2 = c2 - 26
x = x + 2 ;
e = e + 2
c1 = ord ( 'A' ) + wave_height - i - 1
c2 = ord ( 'A' ) + wave_height + i
print ()
wave_height = 4
wave_length = 4
pattern(wave_height, wave_length)
|
C#
using System;
class GFG
{
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 (i = 1; i <= wave_height; i++)
{
for (j = wave_height;
j <= wave_height + i; j++)
{
Console.Write( " " );
}
for (count = 1;
count <= wave_length; count++)
{
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;
if (c1 > 'Z' )
c1 = c1 - 26;
if (c2 > 'Z' )
c2 = c2 - 26;
}
x = x + 2;
e = e + 2;
c1 = 'A' + wave_height - i - 1;
c2 = 'A' + wave_height + i;
Console.WriteLine();
}
}
static public void Main ()
{
int wave_height = 4;
int wave_length = 4;
pattern(wave_height, wave_length);
}
}
|
PHP
<?php
function pattern( $wave_height ,
$wave_length )
{
$e = 2;
$x = 1;
$c1 = 65 + $wave_height - 1;
$c2 = 65 + $wave_height ;
for ( $i = 1; $i <= $wave_height ;
$i ++)
{
for ( $j = $wave_height ;
$j <= $wave_height + $i ;
$j ++)
{
echo " " ;
}
for ( $count = 1;
$count <= $wave_length ;
$count ++)
{
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;
if ( $c1 > 90)
$c1 = $c1 - 26;
if ( $c2 > 90)
$c2 = $c2 - 26;
}
$x = $x + 2;
$e = $e + 2;
$c1 = 65 + $wave_height - $i - 1;
$c2 = 65 + $wave_height + $i ;
echo "\n" ;
}
}
$wave_height = 4;
$wave_length = 4;
pattern( $wave_height , $wave_length );
?>
|
Javascript
<script>
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 (i = 1; i <= wave_height; i++)
{
for (j = wave_height; j <= wave_height + i; j++)
{
document.write( " " );
}
for (count = 1; count <= wave_length; count++)
{
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;
if (c1 > 'Z' .charCodeAt(0))
c1 = c1 - 26;
if (c2 > 'Z' .charCodeAt(0))
c2 = c2 - 26;
}
x = x + 2;
e = e + 2;
c1 = 'A' .charCodeAt(0) + wave_height - i - 1;
c2 = 'A' .charCodeAt(0) + wave_height + i;
document.write( '<br>' );
}
}
var wave_height = 4;
var wave_length = 4;
pattern(wave_height, wave_length);
</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.
Share your thoughts in the comments
Please Login to comment...