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)

Circle Pattern

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.

distance = Math.sqrt((row – rad) * (row – rad) + (col – rad) * (col – rad));

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

Program to Print a Circle Pattern in 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.

/** * 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; int rad; // Accept data from user Scanner sc = new Scanner(System.in); System.out.print("Enter any value for radius: "); rad = sc.nextInt(); 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++) { distance = Math.sqrt((row - rad) * (row - rad) + (col - rad) * (col - rad)); /** * 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 Pattern in 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: "); rad = sc.nextInt(); // Find the diameter of Circle diameter = 2 * rad; // 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 xCoord = rad - row; yCoord = rad - col; point = xCoord * xCoord + yCoord * yCoord; // Check whether point(xCoord, yCoord) is inside of circle or not if (point <= rad * rad + 1) { 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”

Leave a Comment