波形图
给定长度和宽度,使用“/”和“以波形形式打印图案。
例子 :
Input : wave_height = 4
wave_length = 4
Output :
/\ /\ /\ /\
/ \ / \ / \ / \
/ \ / \ / \ / \
/ \/ \/ \/ \
Input : wave_height = 2
wave_length = 3
Output :
/\ /\ /\
/ \/ \/ \
C++
#include
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
= $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
C++
// C++ code to print numbers
// in wave form
#include
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
= $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
C++
#include
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
= $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
输出 :
/\ /\ /\ /\
/ \ / \ / \ / \
/ \ / \ / \ / \
/ \/ \/ \/ \
以波形形式打印数字。
例子 :
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
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
= $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
输出 :
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 = 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
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
= $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
输出 :
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