Star-Alphabet Hybrid Hexagon

This program prints a hexagon combining stars and alphabets alternately.

Problem Statement

Write a Java program that prints a hollow hexagon where odd rows have stars and even rows have alphabets.

Source Code

1 public class StarAlphabetHexagon {
2 public static void main(String[] args) {
3 int n = 6;
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%2==1) System.out.print("* ");
8 else System.out.print((char)('A'+j-1) + " ");
9 }
10 System.out.println();
11 }
12 }
13 }

Program Output

     * * * * * * 
    A B C D E F 
   * * * * * * 
  A B C D E F 
 * * * * * * 
A B C D E F 

Explanation

The condition i%2==1 decides whether the current row prints a row of stars or a row of alphabets, so odd rows show stars and even rows show sequential alphabets, forming an alternating hexagon.

Quick Links to Explore