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

Alphabet Sine Wave Pattern

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
  1. Output
  2. How the alphabet 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

Alphabet Sine Wave Pattern output

Output (3 rows, 3 waves):

  CD    IJ    OP
 B  E  H  K  N  Q
A    FG    LM    R
Alphabet Sine Wave Pattern layout diagram, row by row (3 rows, 3 waves)
How each row of the alphabet sine wave pattern is built (3 rows, 3 waves). Blue cells are printed characters; grey dots are spaces.

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.

  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 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.
  3. 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) % 26 wraps 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.

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 13 of 13 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.