# How to Print a Circle Pattern in Java – Best 2 Ways

In this post, we will learn how to print a circle pattern in Java. You can learn this tutorial for both hollow and solid circular patterns.

## What is a circle pattern?

A star pattern that is represented in a circular manner is called a circle pattern and to develop a program to represent this circle pattern is called a circle pattern program.

Example:

Radius = 4 (Circle pattern image on the right side)

## Simple Logic to Develop Circle Pattern

We have used 2 different for loops for both row and column movement.

First, we will find the distance by using the sqrt( ) function, and then by using this distance, we will check whether the distance is in the range of (radius – 0.5) and (radius + 0.5) or not to print “*”. If it will be in the range of (radius – 0.5) and (radius + 0.5), then we will print “*” otherwise we will print ” ” blank space. After each iteration, we will print a new line.

Also Read: Java Program To Print Vowels In A String – 2 Simple Programs

## Program to Print a Circle Patternin Java(Hollow Circle Pattern)

In this program, we have used the above logic to print a circle pattern. Let’s see the complete program now.

```.wp-block-code {
border: 0;
}

.wp-block-code > div {
overflow: auto;
}

.shcb-language {
border: 0;
clip: rect(1px, 1px, 1px, 1px);
-webkit-clip-path: inset(50%);
clip-path: inset(50%);
height: 1px;
margin: -1px;
overflow: hidden;
position: absolute;
width: 1px;
word-wrap: normal;
word-break: normal;
}

.hljs {
box-sizing: border-box;
}

.hljs.shcb-code-table {
display: table;
width: 100%;
}

.hljs.shcb-code-table > .shcb-loc {
color: inherit;
display: table-row;
width: 100%;
}

.hljs.shcb-code-table .shcb-loc > span {
display: table-cell;
}

.wp-block-code code.hljs:not(.shcb-wrap-lines) {
white-space: pre;
}

.wp-block-code code.hljs.shcb-wrap-lines {
white-space: pre-wrap;
}

.hljs.shcb-line-numbers {
border-spacing: 0;
counter-reset: line;
}

.hljs.shcb-line-numbers > .shcb-loc {
counter-increment: line;
}

.hljs.shcb-line-numbers .shcb-loc > span {
}

.hljs.shcb-line-numbers .shcb-loc::before {
border-right: 1px solid #ddd;
content: counter(line);
display: table-cell;
text-align: right;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
white-space: nowrap;
width: 1%;
}```/**
* Program to Print Circle Pattern in Java
*/
package cf.java.pattern;

import java.util.Scanner;

public class PrintCirclePattern {

public static void main(String[] args) {
// Declare variable for radius and distance
double distance;
// Accept data from user
Scanner sc = new Scanner(System.in);
System.out.print("Enter any value for radius: ");
System.out.println("Circle Pattern:\n");
// Use for loop for row wise
for (int row = 0; row <= 2 * rad; row++) {
// Use for loop for col wise
for (int col = 0; col <= 2 * rad; col++) {

/**
* Check whether distance is in the range of (radius - 0.5) and (radius + 0.5)
* or not to print *
*/
if (distance > rad - 0.5 && distance < rad + 0.5)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}

}
```Code language: PHP (php)```

### Output:

``````Enter any value for radius: 4
Circle Pattern:

*****
**   **
**     **
*       *
*       *
*       *
**     **
**   **
*****
``````

## Simple Logic to Develop a Solid Circle Pattern

As we have to display a solid circle pattern, So we have to think like how we can print that “*” in the circular structure.

So first we will ask the user to enter the radius of circle and then by using this radius, we will find the diameter of the circle. We will use different for loops for both row and column.

We will find both x-coordinate and y-coordinates and then by using these coordinates, we will find each point of the circles, and then we will check whether this point is inside the circle or not. When it will be inside of the circle, we will print the “*” otherwise we will print ” “.

## Program to Print a Circle Patternin Java (Solid Circle Pattern)

In this program, we have used the above logic to print a circle pattern(Solid Circle Pattern). Let’s see the complete program now.

``````/**
* Program to Print a Circle Pattern in Java(Solid Circle Pattern)
*/
package cf.java.pattern;

import java.util.Scanner;

public class PrintSolidCirclePattern {

public static void main(String[] args) {
// Declare all the variables
int diameter, xCoord, yCoord, rad, point;
// Accept the radius from the user
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Radius Of Solid Circle: ");

// Find the diameter of Circle

// Display a circle
System.out.println("Solid Circle:");
for (int row = 0; row <= diameter; row++) {
for (int col = 0; col <= diameter; col++) {
// Define both X-Coordinate and Y-Coordinate

point = xCoord * xCoord + yCoord * yCoord;
// Check whether point(xCoord, yCoord) is inside of circle or not
System.out.print("* ");
} else {
System.out.print("  ");
}
}
// Print a new line
System.out.println();
}

}

}
```Code language: PHP (php)```

### Output:

``````Enter the Radius Of Solid Circle: 5
Solid Circle:
* * *
* * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * * * *
* * * * * * * * *
* * * * * * * * *
* * * * * * *
* * *
``````

## Other Queries We Have Covered

• Java Program to Print Circle Pattern.
• Java Program to Print Circle Shaped Star Pattern.
• How to Create a Circle in Java?
• How to Draw a Circle in Java Using Loops?

## FAQs

### What is a circle pattern?

A star pattern that is represented in a circular manner is called a circle pattern and to develop a program to represent this circle pattern is called a circle pattern program.

### How to Fidn the Diameter of a Circle in Java?

We can find the diameter of a circle by multiplying the radius with 2.
Formula: Diameter = 2 * radius

### What is the Radius of a Circle?

Radius is the distance from center of a circle to any point of circuference of circle. We can ask user to provide the radius to fidn the circle in Java.

### 2 thoughts on “How to Print a Circle Pattern in Java – Best 2 Ways”

1. The article is to the point and easy to understand, I like the “for loop” by W3Schools. One suggestion, “sqrt( ) function” explanation by GFG not up to the mark rather this article explains it better: https://www.scaler.com/topics/square-root-in-java/