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
Number Sine Wave Pattern output
Output (3 rows, 3 waves):
34 91 67 2 5 8 2 5 8 1 67 34 9

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 β¦
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
6 Γ npositions 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. - 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 + 1keeps 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.






