Concentric Alphabet Square

This program prints concentric squares using alphabets.

Problem Statement

Write a Java program that prints concentric alphabet squares in a 5x5 grid.

Source Code

1 public class ConcentricAlphabetSquare {
2 public static void main(String[] args) {
3 int n = 5;
4 for(int i=0; i<n; i++) {
5 for(int j=0; j<n; j++) {
6 int min = Math.min(Math.min(i, j), Math.min(n-1-i, n-1-j));
7 char ch = (char)('A' + min);
8 System.out.print(ch + " ");
9 }
10 System.out.println();
11 }
12 }
13 }

Program Output

A A A A A 
A B B B A 
A B C B A 
A B B B A 
A A A A A 

Explanation

The distance min from the border is computed as before, and a character 'A'+min is derived from it, so concentric layers are printed with alphabets instead of numbers, demonstrating mathematical logic combined with character arithmetic.

Quick Links to Explore