Hollow Concentric Circle Approximation

This program approximates concentric circles using stars.

Problem Statement

Write a Java program that prints concentric circles approximated in text with 9x9 grid.

Source Code

1 public class HollowConcentricCircle {
2 public static void main(String[] args) {
3 int n = 9;
4 for(int i=0; i<n; i++) {
5 for(int j=0; j<n; j++) {
6 int dist = Math.abs(i-n/2)+Math.abs(j-n/2);
7 if(dist==4 || dist==2 || dist==0) System.out.print("* ");
8 else System.out.print(" ");
9 }
10 System.out.println();
11 }
12 }
13 }

Program Output

    * * *     
   *     *    
  *       *   
 *         *  
*           * 
 *         *  
  *       *   
   *     *    
    * * *     

Explanation

The Manhattan distance from the grid center is computed for every cell, and stars are printed only at specific distance values, so multiple concentric ring-like layers of stars approximate circles in a text grid.

Quick Links to Explore