Hollow Concentric Octagon with Hybrid Symbols

This program prints hollow concentric octagons alternating stars, numbers, and alphabets.

Problem Statement

Write a Java program that prints concentric hollow octagons where boundaries alternate stars, numbers, and alphabets cyclically.

Source Code

1 public class HollowConcentricOctagonHybridSymbols {
2 public static void main(String[] args) {
3 int n=7,num=1; char ch='A'; int cycle=0;
4 for(int i=0;i<n;i++){
5 for(int j=0;j<n;j++){
6 if(i==0||i==n-1||j==0||j==n-1||i==j||i+j==n-1){
7 if(cycle%3==0) System.out.print("* ");
8 else if(cycle%3==1) System.out.print(num+++" ");
9 else {
10 System.out.print(ch+" ");
11 ch++;
12 if(ch>'Z') ch='A';
13 }
14 cycle++;
15 } else System.out.print(" ");
16 }
17 System.out.println();
18 }
19 }
20 }

Program Output

* 1 A * 2 B * 
C * 3 D * 4 E 
F   G   H   I 
*     J     * 
K   L   M   N 
O * P * Q * R 
* S T U V W X 

Explanation

A running cycle counter increments only at boundary/diagonal cells and its value modulo 3 decides whether a star, number, or alphabet is printed, so the octagon boundary itself cycles through three different symbol types in sequence.

Quick Links to Explore