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

Number Sine Wave Pattern

The number sine wave draws the same rounded wave as the star sine wave, but each point of the curve shows a digit. Following the curve from left to right you read 1, 2, 3 up to 9 and then start again at 1.

Quick answer: Use the star sine wave condition to decide where the curve is: position (j βˆ’ 1) % 2n equal to n βˆ’ i or n + i βˆ’ 1. Instead of a star, print (j βˆ’ 1) % 9 + 1, a digit that follows the column and cycles through 1 to 9.

This program is part of the Wave Patterns collection.

Table of contents8 sections
  1. Output
  2. How the number 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

Number Sine Wave Pattern output

Output (3 rows, 3 waves):

  34    91    67
 2  5  8  2  5  8
1    67    34    9
Number Sine Wave Pattern layout diagram, row by row (3 rows, 3 waves)
How each row of the number sine wave pattern is built (3 rows, 3 waves). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Star 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 number sine wave pattern works

The curve has exactly one point in every column, so numbering the points by column numbers them in the order you read the wave. Taking the column modulo 9 keeps every label a single digit, which stops the wave from spreading out when numbers reach 10. Reading the output along the curve gives 1 2 3 4 5 6 7 8 9 1 2 …

  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 numbers (value (j βˆ’ 1) % 9 + 1) 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

  • The curve has one point per column, so the column number labels it in order.
  • (j βˆ’ 1) % 9 + 1 keeps every label to one digit.
  • The shape comes from the same condition as the star sine wave.
  • Reading the wave from left to right counts 1 to 9 over and over.

Number 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("%d", (j - 1) % 9 + 1);
            else
                printf(" ");
        }
        printf("\n");
    }
    return 0;
}

Number 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 << (j - 1) % 9 + 1;
            else
                cout << " ";
        }
        cout << endl;
    }
    return 0;
}

Number Sine Wave Pattern in Java

public class NumberSineWave {
    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((j - 1) % 9 + 1);
                else
                    System.out.print(" ");
            }
            System.out.println();
        }
    }
}

Number 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((j - 1) % 9 + 1, 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

Why do the numbers restart after 9?

Two-digit numbers would take two columns and break the curve. Cycling through 1 to 9 keeps one character per column.

How do I print letters instead?

Print 'A' + (j βˆ’ 1) % 26. The wave then spells the alphabet along its curve.

How do I make it a straight zig-zag?

Use the three-row zig-zag condition from the number zig-zag wave instead of the sine condition.

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 10 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.