Program to print circle pattern
Given a positive integer n i.e, radius of the circle, print a circle using stars.
Examples :
Input : n = 3
Output :
***
* *
* *
* *
* *
* *
***
Input : n = 6
Output :
*****
** **
** **
* *
* *
* *
* *
* *
* *
* *
** **
** **
*****
CPP
// C++ implementation to print circle pattern #include <bits/stdc++.h> using namespace std; // function to print circle pattern void printPattern(int radius) { // dist represents distance to the center float dist; // for horizontal movement for (int i = 0; i <= 2 * radius; i++) { // for vertical movement for (int j = 0; j <= 2 * radius; j++) { dist = 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) cout << "*"; else cout << " "; } cout << "\n"; } } // Driver Code int main() { int radius = 6; printPattern(radius); return 0; } |
chevron_right
filter_none
Java
// 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); } } // This code is contributed by Anant Agarwal. |
chevron_right
filter_none
Python3
# Python implementation # to print circle pattern import math # function to print circle pattern def printPattern(radius): # dist represents distance to the center # for horizontal movement for i in range((2 * radius)+1): # for vertical movement for j in range((2 * radius)+1): 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 and dist < radius + 0.5): print("*",end="") else: print(" ",end="") print() # Driver code radius = 6printPattern(radius) # This code is contributed # by Anant Agarwal. |
chevron_right
filter_none
C#
// C# implementation to print circle pattern using System; 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) Console.Write("*"); else Console.Write(" "); } Console.WriteLine(""); } } // Driver code public static void Main() { int radius = 6; printPattern(radius); } } // This code is contributed by vt_m. |
chevron_right
filter_none
PHP
<?php // PHP implementation to print // circle pattern // Function to print circle pattern function printPattern($radius) { // dist represents distance // to the center $dist = 0.0; // for horizontal movement for ($i = 0; $i <= 2 * $radius; $i++) { // for vertical movement for ($j = 0; $j <= 2 * $radius; $j++) { $dist = 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) echo "*"; else echo " "; } echo "\n"; } } // Driver Code $radius = 6; printPattern($radius); // This code is contributed by Mithun Kumar ?> |
chevron_right
filter_none
Output :
*****
** **
** **
* *
* *
* *
* *
* *
* *
* *
** **
** **
*****
Recommended Posts:
- Program to print the given H Pattern
- Program to print the pattern ‘D’
- Program to print the pattern "GFG"
- Program to print the given Z Pattern
- Program to print pattern
- Program to print the pattern 'G'
- Program to print Hut Star pattern
- Program to print the arrow pattern
- Program to print V and inverted-V pattern
- Program to print a pattern of numbers
- Program to print pyramid pattern
- Program to print Crown Pattern
- Program to Print the Trapezium Pattern
- Program to print numeric pattern | Set - 2
- Program to print numeric pattern
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : Mithun Kumar



