The alphabet sine wave writes the alphabet along a rounded wave: reading the curve from left to right gives A, B, C, D and so on. It uses the same shape as the star sine wave.
Quick answer: Each wave is 2n columns wide. On row i, the curve passes the columns where (j β 1) % 2n equals n β i or n + i β 1. Print the letter 'A' + (j β 1) % 26 there, so the letters follow the columns.
This program is part of the Wave Patterns collection.
Table of contents8 sections
Alphabet Sine Wave Pattern output
Output (3 rows, 3 waves):
CD IJ OP B E H K N Q A FG LM R

How the alphabet sine wave pattern works
The curve has exactly one point in every column, so labelling each point with its column number spells the alphabet along the wave in order. Taking the column modulo 26 starts again at A after Z, which keeps long waves working. The shape itself comes from the same two positions per wave as the star version.
- Run the outer loop for
ifrom1ton, one row per pass. - Go through
6 Γ npositions in the row. Print letters (letter number(j β 1) % 26 + 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 letter per column, so the letters read in order along it.
- The wave shape uses the same two positions per wave as the star sine wave.
(j β 1) % 26wraps back to A after Z.- It is the letter version of the number sine wave.
Alphabet 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("%c", 'A' + (j - 1) % 26 + 1 - 1);
else
printf(" ");
}
printf("\n");
}
return 0;
}Alphabet 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 << char('A' + (j - 1) % 26 + 1 - 1);
else
cout << " ";
}
cout << endl;
}
return 0;
}Alphabet Sine Wave Pattern in Java
public class AlphabetSineWave {
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((char) ('A' + (j - 1) % 26 + 1 - 1));
else
System.out.print(" ");
}
System.out.println();
}
}
}Alphabet 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(chr(ord('A') + (j - 1) % 26 + 1 - 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 letters appear in pairs at the top and bottom?
The rounded peaks and troughs are two columns wide, so two neighbouring letters, such as C and D, sit side by side there.
How do I make the wave taller?
Raise n. Each wave then has n rows and 2n columns, so more letters fit on each rise and fall.
Can I write a word along the wave instead?
Yes. Print word[(j β 1) % length] instead of the alphabet letter, and the word repeats along the curve.






