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
Alphabet Square Pattern output
Output (n = 4):
A A A A B B B B C C C C D D D D

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.
- Run the outer loop for
ifrom1ton, one row per pass. - Print
nletters (letter numberi). - Move to the next line.
Key takeaways
- Both loops run
ntimes, giving a square. - The letter comes from the row:
'A' + i − 1. - Using
jinstead makes every row read A B C D. - Adding
iandjgives 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.






