Print Circle Pattern in Java

You are currently viewing Print Circle Pattern in Java

Hello Geeks! If you are looking to learn the concept of print circle pattern in java then read the tutorial to learn about it. If you can give the radius of a circle in positive integer then the circle pattern will print on the screen with stars using the Java program. Let’s move on to this article.

Print Circle Pattern in Java

We can print circle pattern using java program here.

// Java implementation to print circle pattern

class GFG {
	
// function to print circle pattern
static void printPattern(int radius) {

	// dist represents distance to the center
	double dist;

	// for horizontal movement
	for (int i = 0; i <= 2 * radius; i++) {

	// for vertical movement
	for (int j = 0; j <= 2 * radius; j++) {
		dist = Math.sqrt((i - radius) * (i - radius) +
						(j - radius) * (j - radius));

		// dist should be in the range (radius - 0.5)
		// and (radius + 0.5) to print stars(*)
		if (dist > radius - 0.5 && dist < radius + 0.5)
		System.out.print("*");
		else
		System.out.print(" ");
	}

	System.out.print("\n");
	}
}

// Driver code
public static void main(String[] args)
{
	int radius = 6;
	printPattern(radius);
}
}

How to Execute a Program?

Initially, We have to declare the class name into this program. Secondly, We need to set the range of both vertical and horizontal movement of a circle pattern using a for loop.

Now, The if…else condition to check the both vertical and horizontal radius range of a circle pattern. If the vertical and horizontal radius range is greater than and less than ( -0.5 and +0.5 ) respectively then the if…condition will print the circle pattern as output.

Otherwise, It will print an empty space as output. Finally, We will see the output on the screen using the system.out.printIn() function.

Read Also : How to Auto Calculate Price in Javascript?

Output

The output of the print circle pattern in java will be provided below for your reference.

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

Final Thoughts

I hope this Java article helps you to do “ Print Circle Pattern in Java ”. If you have any doubts regarding this topic then please let us know via the comment section. We will help you. Share this article with other Java developers.

Manoj S

Hi, I'm Manoj working as a part-time blogger in Coding Diksha. Here, I post about programming to help developers.

Leave a Reply