The star sine wave draws a smooth up-and-down curve with stars, with a flat pair of stars at each peak and each trough. Three rows and three waves already look like the sine curve from a maths book.
Quick answer: One wave is 2n columns wide. On row i (1 at the top), put a star where the position inside the wave, (j β 1) % 2n, equals n β i or n + i β 1. The top row gets the middle pair of each wave; the bottom row gets the two ends.
This program is part of the Wave Patterns collection.
Table of contents8 sections
Star Sine Wave Pattern output
Output (3 rows, 3 waves):
** ** ** * * * * * * * ** ** *

Same shape, other characters: the Number Sine Wave Pattern program draws this same outline with different characters. Putting them side by side shows which part of the loop decides the shape and which part decides what is printed.
How the star sine wave pattern works
Inside one wave the curve goes up for n columns and comes down for n columns. On the way up it passes row i at position n β i, and on the way down at n + i β 1. The remainder by 2n repeats this for every wave, so one condition draws the whole line. The two neighbouring positions at the top and bottom give the rounded peaks.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
6 Γ npositions in the row. Print stars where(j β 1) % (2 Γ n) = n β i or (j β 1) % (2 Γ n) = n + i β 1; print spaces everywhere else. - Move to the next line.
Key takeaways
- Each wave is
2ncolumns wide. - Every row has two stars per wave: one going up, one coming down.
- The remainder
(j β 1) % 2nrepeats the wave across the line. - Change
6 * nto2n Γ wavesfor more or fewer waves.
Star Sine Wave Pattern in C
#include <stdio.h>
int main(void) {
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 6 * n; j++) {
if ((j - 1) % (2 * n) == n - i || (j - 1) % (2 * n) == n + i - 1)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}Star Sine Wave Pattern in C++
#include <iostream>
using namespace std;
int main() {
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 6 * n; j++) {
if ((j - 1) % (2 * n) == n - i || (j - 1) % (2 * n) == n + i - 1)
cout << "*";
else
cout << " ";
}
cout << endl;
}
return 0;
}Star Sine Wave Pattern in Java
public class StarSineWave {
public static void main(String[] args) {
int n = 3;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= 6 * n; j++) {
if ((j - 1) % (2 * n) == n - i || (j - 1) % (2 * n) == n + i - 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}Star Sine Wave Pattern in Python
n = 3
for i in range(1, n + 1):
for j in range(1, 6 * n + 1):
if (j - 1) % (2 * n) == n - i or (j - 1) % (2 * n) == n + i - 1:
print("*", end="")
else:
print(" ", end="")
print()Time and space complexity
The program prints one character for each position in the pattern, so for n rows it runs in O(nΒ²) time. It only needs a few loop variables, so the extra memory is O(1).
Frequently asked questions
How is it different from the zig-zag star wave?
The zig-zag star wave has single stars at its tips, so it looks sharp. This wave puts two stars side by side at each peak and trough, which makes it look rounded.
How do I make the wave taller?
Raise n. Each wave then has n rows and is 2n columns wide.
Can I print numbers along the wave?
Yes, see the number sine wave, which prints the column number on each point of the curve.






