Hollow Concentric Circle with Numbers

This program approximates concentric circles using numbers.

Problem Statement

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

Source Code

1 public class HollowConcentricCircleNumbers {
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(i+1 + " ");
8 else System.out.print(" ");
9 }
10 System.out.println();
11 }
12 }
13 }

Program Output

    1 1 1     
   2     2    
  3       3   
 4         4  
5           5 
 6         6  
  7       7   
   8     8    
    9 9 9     

Explanation

The same Manhattan-distance ring logic is used, but instead of a fixed star, the current row index (i+1) is printed at the specified distances, so numeric rings approximate the concentric circles.

Quick Links to Explore