Program to print Sine-Wave Pattern
Last Updated :
02 May, 2025
Give the Height and Width of a Wave to print the Sine wave pattern
Examples:
Input : wave_height=5
wave_length=10
Output :
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Approach:
First, check the row and column where the elements are needed to be printed. Then, use nested for loops to print the elements in the corresponding order. Separate loops are kept to keep track of the wave_height and wave_length.
C++
// C++ program to print sign wave pattern.
#include <bits/stdc++.h>
using namespace std;
void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
{
cout << " ";
}
// put any symbol
cout << "0";
for (int k = 1; k <= is; k++)
cout << " ";
// put any symbol
cout << "0";
for (int k = 1; k <= os; k++)
cout << " ";
cout << " ";
}
// set a value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not equal
// to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
cout << "\n";
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
// This code is contributed by shivanisinghss2110
C
// C program to print sign wave pattern.
#include <stdio.h>
void printSinWave(int wave_height, int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++) {
// for loop for wave length
for (int j = 1; j <= wave_length; j++) {
// intermediate spaces
for (int k = 1; k <= os; k++) {
printf(" ");
}
// put any symbol
printf("0");
for (int k = 1; k <= is; k++)
printf(" ");
// put any symbol
printf("0");
for (int k = 1; k <= os; k++)
printf(" ");
printf(" ");
}
// set a value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not equal
// to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
printf("\n");
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
Java
// Java program to print
// sign wave pattern.
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
// for loop for height of wave
for (int i = 1;
i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1;
j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
{
System.out.printf(" ");
}
// put any symbol
System.out.printf("0");
for (int k = 1; k <= is; k++)
System.out.printf(" ");
// put any symbol
System.out.printf("0");
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf(" ");
}
// set a value of os to 1 if i+1
// is not equal to wave height
// or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
System.out.printf("\n");
}
}
// Driver code
public static void main(String []args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by Smitha
Python
# Python3 program to print sign wave pattern.
def printSinWave(wave_height, wave_length):
# inner space and outer space.
Is = 1
os = 2
# for loop for height of wave
for i in range(1, wave_height + 1):
# for loop for wave length
for j in range(1, wave_length + 1):
# intermediate spaces
for k in range(1, os + 1):
print(end = " ")
# put any symbol
print("0", end = "")
for k in range(1, Is + 1):
print(end = " ")
# put any symbol
print("0", end = "")
for k in range(1, os + 1):
print(end = " ")
print(end = " ")
# set a value of os to 1 if i+1 Is not
# equal to wave height or 0 otherwise
if (i + 1 != wave_height):
os = 1
else:
os = 0
# set value of Is to 3 if i+1 Is not
# equal to wave height or 5 otherwise
if (i + 1 != wave_height):
Is = 3
else:
Is = 5
print()
# Driver code
wave_height, wave_length = 5, 10
printSinWave(wave_height, wave_length)
# This code is contributed by
# Mohit kumar 29
C#
// C# program to print
// sign wave pattern.
using System;
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int Is = 1, os = 2;
// for loop for height of wave
for (int i = 1;
i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1;
j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
{
Console.Write(" ");
}
// put any symbol
Console.Write("0");
for (int k = 1; k <= Is; k++)
Console.Write(" ");
// put any symbol
Console.Write("0");
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write(" ");
}
// set a value of os to 1 if i+1
// is not equal to wave height
// or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
Is = (i + 1 != wave_height) ? 3 : 5;
Console.Write("\n");
}
}
// Driver code
public static void Main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed
// by Akanksha Rai(Abby_akku)
JavaScript
<script>
// JavaScript program to print
// sign wave pattern.
function printSinWave(wave_height, wave_length)
{
// inner space and outer space.
var is = 1,
os = 2;
// for loop for height of wave
for (var i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (var j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (var k = 1; k <= os; k++)
{
document.write(" ");
}
// put any symbol
document.write("0");
for (var k = 1; k <= is; k++)
document.write(" ");
// put any symbol
document.write("0");
for (var k = 1; k <= os; k++)
document.write(" ");
document.write(" ");
}
// set a value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = i + 1 != wave_height;
// set value of is to 3 if i+1 is not equal
// to wave height or 5 otherwise
is = i + 1 != wave_height ? 3 : 5;
document.write("<br>");
}
}
// Driver code
var wave_height = 5,
wave_length = 10;
printSinWave(wave_height, wave_length);
</script>
Output 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Time complexity: O(h*l), Here h and l are wave height and wave length respectively.
Auxiliary space: O(1), As constant extra space is used.
How to print a sin wave of letters?
Input : wave_height=5
wave_length=10
Output :
E F O P Y Z I J S T C D M N W X G H Q R
D G N Q X A H K R U B E L O V Y F I P S
C H M R W B G L Q V A F K P U Z E J O T
B I L S V C F M P W Z G J Q T A D K N U
A J K T U D E N O X Y H I R S B C L M V
The approach is the same as above, now instead of using a single element, we use all of the 26 English alphabets in Upper_Case.
C++
// C++ program to print sign wave pattern.
#include <stdio.h>
void printSinWave(int wave_height, int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) - (wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++) {
// for loop for wave length
for (int j = 1; j <= wave_length; j++) {
// intermediate spaces
for (int k = 1; k <= os; k++)
printf(" ");
printf("%c", ch);
for (int k = 1; k <= is; k++)
printf(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
printf("%c", ch);
for (int k = 1; k <= os; k++)
printf(" ");
printf(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height);
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
printf("\n");
}
}
// Driver code
int main()
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
return 0;
}
Java
// Java program to print sign wave pattern.
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int is = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) -
(wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf("%c", ch);
for (int k = 1; k <= is; k++)
System.out.printf(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
System.out.printf("%c", ch);
for (int k = 1; k <= os; k++)
System.out.printf(" ");
System.out.printf(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
System.out.printf("\n");
}
}
// Driver code
public static void main(String[] args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by PrinciRaj1992
Python
# Python program to print sign wave pattern.
def printSinWave(wave_height, wave_length):
# inner space and outer space.
Is = 1
os = 2
inc = 1
jump = (wave_height * 3) -(wave_height + 1)
ch = ord('A') + wave_height - 1
# for loop for height of wave
for i in range(1, wave_height + 1):
# for loop for wave length
for j in range(1, wave_length + 1):
# intermediate space
for k in range(1, os + 1):
print(end = " ")
print(chr(ch), end = "")
for k in range(1, Is + 1):
print(end = " ")
ch += inc
if(ch > ord('Z')):
ch = ch - 26
print(chr(ch), end = "")
for k in range(1, os + 1):
print(end = " ")
print(end = " ")
ch += jump
if(ch > ord('Z')):
ch = ch - 26
# set value of os to 1 if i+1 is not
# equal to wave height or 0 otherwise
if(i + 1 != wave_height):
os = 1
else:
os = 0
# set value of is to 3 if i+1 is not
# equal to wave height or 5 otherwise
if(i + 1 != wave_height):
Is = 3
else:
Is = 5
ch = ord('A') + wave_height - i - 1
inc = inc + 2
jump -= 2
print()
# Driver code
wave_height = 5
wave_length = 10
printSinWave(wave_height, wave_length)
# This code is contributed by avanitrachhadiya2155
C#
// C# program to print sign wave pattern.
using System;
class GFG
{
static void printSinWave(int wave_height,
int wave_length)
{
// inner space and outer space.
int iS = 1, os = 2;
int inc = 1;
int jump = (wave_height * 3) -
(wave_height + 1);
int ch = 'A' + wave_height - 1;
// for loop for height of wave
for (int i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (int j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write("{0}", (char)ch);
for (int k = 1; k <= iS; k++)
Console.Write(" ");
ch += inc;
if (ch > 'Z')
ch = ch - 26;
Console.Write("{0}", (char)ch);
for (int k = 1; k <= os; k++)
Console.Write(" ");
Console.Write(" ");
ch += jump;
if (ch > 'Z')
ch = ch - 26;
}
// set value of os to 1 if i+1 iS not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of iS to 3 if i+1 iS not
// equal to wave height or 5 otherwise
iS = (i + 1 != wave_height) ? 3 : 5;
ch = 'A' + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
Console.Write("\n");
}
}
// Driver code
public static void Main(String[] args)
{
int wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
}
}
// This code is contributed by Princi Singh
JavaScript
<script>
// Javascript program to print sign wave pattern.
function printSinWave(wave_height,wave_length)
{
// inner space and outer space.
let is = 1, os = 2;
let inc = 1;
let jump = (wave_height * 3) -
(wave_height + 1);
let ch = 'A'.charCodeAt(0) + wave_height - 1;
// for loop for height of wave
for (let i = 1; i <= wave_height; i++)
{
// for loop for wave length
for (let j = 1; j <= wave_length; j++)
{
// intermediate spaces
for (let k = 1; k <= os; k++)
document.write("  ");
document.write(String.fromCharCode(ch));
for (let k = 1; k <= is; k++)
document.write("  ");
ch += inc;
if (ch > 'Z'.charCodeAt(0))
ch = ch - 26;
document.write(String.fromCharCode(ch));
for (let k = 1; k <= os; k++)
document.write("  ");
document.write("  ");
ch += jump;
if (ch > 'Z'.charCodeAt(0))
ch = ch - 26;
}
// set value of os to 1 if i+1 is not
// equal to wave height or 0 otherwise
os = (i + 1 != wave_height) ? 1 : 0;
// set value of is to 3 if i+1 is not
// equal to wave height or 5 otherwise
is = (i + 1 != wave_height) ? 3 : 5;
ch = 'A'.charCodeAt(0) + wave_height - i - 1;
inc = inc + 2;
jump -= 2;
document.write("<br>");
}
}
// Driver code
let wave_height = 5, wave_length = 10;
printSinWave(wave_height, wave_length);
// This code is contributed by patel2127
</script>
Time complexity: O(h*w) where h and w are wave height and wave width respectively
Auxiliary space: O(1) as it is using constant space for variables
Similar Reads
Program to print Spiral Pattern
Given a number n as the size of the matrix, the task is to print a spiral pattern in 2D array of size n. Examples: Input: n = 5 Output: 1 2 3 4 5 16 17 18 19 6 15 24 25 20 7 14 23 22 21 8 13 12 11 10 9 Preview of the Spiral Pattern: Approach: Create a 2D array of size nStore the boundary of the arra
10 min read
Program to print pattern
Given the value of n, print the following pattern.Examples : Input : n = 4 Output : A1 AB12 ABC123 ABCD1234 Input : n = 7 Output : A1 AB12 ABC123 ABCD1234 ABCDE12345 ABCDEF123456 ABCDEFG1234567 Below is the implementation to print the above pattern : C/C++ Code // C++ program to print given pattern
5 min read
Program to Print Mirror Image of Sine-Wave Pattern
A sine wave is a mathematical curve that oscillates between a maximum and minimum value, smoothly transitioning between positive and negative values. It is commonly used to represent periodic phenomena such as sound, light, and electrical signals. Examples: Give the Height and Width of a Wave to pri
5 min read
Program to Print the Trapezium Pattern
Given 'num' which indicates number of lines.The task is to print a trapezium pattern in num lines.Examples: Input : 4 Output : 1*2*3*4*17*18*19*20 5*6*7*14*15*16 8*9*12*13 10*11 Input : 2 Output : 1*2*5*6 3*4 Algorithm : step 1. To read num which indicates the number of lines. step 2.We are diving t
6 min read
Program to print the Zigzag pattern
Given a number N denoting the number of rows. The task is to print the zigzag pattern with N rows as shown in the below examples.Examples: Input : N = 3 Output : 1 3*2 4*5*6 Input : N = 5 Output : 1 3*2 4*5*6 10*9*8*7 11*12*13*14*15 Approach: Use a for loop for printing the number of rows.Use two va
6 min read
Program to print the pattern "GFG"
In this article, given the value of n(length of the alphabet) and k(width of the alphabet) we will learn how to print the pattern "GFG" using stars and white-spaces. Examples: INPUT: n=7, k=5 OUTPUT: ***** ***** ***** * * * * * * * ** ***** * *** * * * * * * * * * * ***** * ***** INPUT: n=11, k=7 OU
8 min read
Program to print pyramid pattern
Write to program to print the pyramid pattern formed of stars Example : Input: n = 6 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * We strongly recommend you to minimize your browser and try this yourself first.The idea is to use two for loops for every part of the p
5 min read
Program to print the arrow pattern
Given the value of n, print the arrow pattern.Examples : Input : n = 5 Output : * ** *** **** ***** **** *** ** * Input : n = 7 Output : * ** *** **** ***** ****** ******* ****** ***** **** *** ** * Below is the program to print the arrow pattern: C/C++ Code // C++ program to print the // arrow patt
10 min read
Program to print numeric pattern | Set - 2
Given a number as 'num', and a number of lines as 'num_of_lines' where 'num' implies the starting number from which the pattern has to be started and 'num_of_lines' implies the number of lines that have to be printed. Now, according to the above information, print a pattern as given below. Examples:
5 min read
Recursive program to print triangular patterns
We have discussed iterative pattern printing in previous post. Examples: Input : 7 Output : * * * * * * * * * * * * * * * * * * * * * * * * * * * * Algorithm:- step 1:- first think for the base condition i.e. number less than 0 step 2:-do the recursive calls till number less than 0 i.e:- printPartte
8 min read