New: 214 pattern programs with tested code in C, C++, Java and Python Explore patterns

Star Sine Wave Pattern

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
  1. Output
  2. How the star sine wave pattern works
  3. Program in C
  4. Program in C++
  5. Program in Java
  6. Program in Python
  7. Time and space complexity
  8. Frequently asked questions

Star Sine Wave Pattern output

Output (3 rows, 3 waves):

  **    **    **
 *  *  *  *  *  *
*    **    **    *
Star Sine Wave Pattern layout diagram, row by row (3 rows, 3 waves)
How each row of the star sine wave pattern is built (3 rows, 3 waves). Blue cells are printed characters; grey dots are spaces.

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.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Go through 6 Γ— n positions in the row. Print stars where (j βˆ’ 1) % (2 Γ— n) = n βˆ’ i or (j βˆ’ 1) % (2 Γ— n) = n + i βˆ’ 1; print spaces everywhere else.
  3. Move to the next line.

Key takeaways

  • Each wave is 2n columns wide.
  • Every row has two stars per wave: one going up, one coming down.
  • The remainder (j βˆ’ 1) % 2n repeats the wave across the line.
  • Change 6 * n to 2n Γ— waves for 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.

This tutorial was drafted with AI assistance. Spotted a mistake? Tell us and we fix it. Read our editorial policy.

Quick check: can you predict the output?

Answer from the code, then check. Every answer comes from the real output above.

Pattern 9 of 12 in Wave Patterns

 

Still stuck? Ask AI to explain this code

Opens your favourite AI with this page, ready to explain every loop step by step.

Authored by

Ghanendra Yadav runs Pattern Programs from Bangalore, India. The site started in 2020 to help students practise loops and logic through pattern programs in C, C++, Java and Python. Every program published here is compiled and run before it goes live, and the output shown is the real output.

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.