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

Alphabet Square Pattern

The alphabet square prints one letter per row, repeated across the row: A A A A, then B B B B, and so on. It is the letter version of the row number square (1111, 2222).

Quick answer: Use two loops that both run n times. Inside, print 'A' + i − 1 followed by a space. The letter depends only on the row, so every position on a row shows the same letter: four As on the first row, four Bs on the second, and so on down to D.

This program is part of the String Patterns collection.

Table of contents8 sections
  1. Output
  2. How the alphabet square 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 Square Pattern output

Output (n = 4):

A A A A
B B B B
C C C C
D D D D
Alphabet Square Pattern layout diagram, row by row (n = 4)
How each row of the alphabet square pattern is built (n = 4). Blue cells are printed characters; grey dots are spaces.

Same shape, other characters: the Alphabet Spiral Matrix 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 alphabet square pattern works

A square needs the same number of rows and columns, so both loops run n times. Picking the letter from the row counter i makes each row uniform. If you pick it from the column counter j instead, every row reads A B C D, and the letters repeat down the columns.

  1. Run the outer loop for i from 1 to n, one row per pass.
  2. Print n letters (letter number i).
  3. Move to the next line.

Key takeaways

  • Both loops run n times, giving a square.
  • The letter comes from the row: 'A' + i − 1.
  • Using j instead makes every row read A B C D.
  • Adding i and j gives diagonal stripes of letters.

Alphabet Square Pattern in C

#include <stdio.h>

int main(void) {
    int n = 4;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            printf("%c ", 'A' + i - 1);
        printf("\n");
    }
    return 0;
}

Alphabet Square Pattern in C++

#include <iostream>
using namespace std;

int main() {
    int n = 4;

    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++)
            cout << char('A' + i - 1) << " ";
        cout << endl;
    }
    return 0;
}

Alphabet Square Pattern in Java

public class AlphabetSquare {
    public static void main(String[] args) {
        int n = 4;

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++)
                System.out.print((char) ('A' + i - 1) + " ");
            System.out.println();
        }
    }
}

Alphabet Square Pattern in Python

n = 4

for i in range(1, n + 1):
    for j in range(1, n + 1):
        print(chr(ord('A') + i - 1), 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 do I make every row read A B C D?

Print 'A' + j − 1. The letter then follows the column, so each row spells the start of the alphabet.

What does ‘A’ + i + j − 2 print?

Rows that each start one letter later: A B C D, B C D E, C D E F. Each diagonal from bottom left to top right holds one letter.

How do I let the user choose the size?

Read n from the keyboard instead of fixing it in the code: scanf("%d", &n); in C, cin >> n; in C++, new Scanner(System.in).nextInt() in Java and int(input()) in Python.

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 12 of 22 in String 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.