Hollow Concentric Hexagon with Alphabets
This program prints a hollow concentric hexagon using alphabets.
Problem Statement
Write a Java program that prints a hollow concentric hexagon with alphabets up to 7 rows.
Source Code
| 1 | public class HollowConcentricHexagonAlphabets { |
| 2 | public static void main(String[] args) { |
| 3 | int n = 7; |
| 4 | for(int i=1; i<=n; i++) { |
| 5 | for(int j=i; j<n; j++) System.out.print(" "); |
| 6 | for(int j=1; j<=n; j++) { |
| 7 | if(i==1 || i==n || j==1 || j==n || i==n/2+1) |
| 8 | System.out.print((char)('A'+i-1) + " "); |
| 9 | else System.out.print(" "); |
| 10 | } |
| 11 | System.out.println(); |
| 12 | } |
| 13 | } |
| 14 | } |
Program Output
A A A A A A A
B B
C C
D D D D D D D
E E
F F
G G G G G G G
Explanation
The hollow hexagon boundary and middle-row logic is reused, but the printed symbol is now an alphabet derived from the row index rather than a fixed star, labeling each boundary row with its own letter.