Program to print circle pattern

09 Apr, 2021
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
Java
Python3
C#
PHP
Javascript
// C++ implementation to print circle pattern#include <bits/stdc++.h>using namespace std; // function to print circle patternvoid 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 Codeint main() {  int radius = 6;  printPattern(radius);  return 0;}
// Java implementation to print circle pattern class GFG {     // function to print circle patternstatic 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 codepublic static void main(String[] args){    int radius = 6;    printPattern(radius);}} // This code is contributed by Anant Agarwal.
# Python implementation# to print circle pattern import math # function to print circle patterndef 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.
// C# implementation to print circle patternusing 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.
<?php// PHP implementation to print// circle pattern // Function to print circle patternfunction 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?>
<script> // JavaScript implementation to print circle pattern // function to print circle patternfunction printPattern(radius){// dist represents distance to the center        var dist = parseFloat(0);         // for horizontal movement        for (var i = 0; i <= 2 * radius; i++)        {           // for vertical movement          for (var 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)            document.write("*");            else            document.write("  ");          }           document.write("<br>");        }      }       // Driver Code             var radius = 6;        printPattern(radius);           </script>
    *****    
  **     **  
 **       ** 
 *         * 
*           *
*           *
*           *
*           *
*           *
 *         * 
 **       ** 
  **     **  
    *****  
 
Mithun Kumar
rdtank
circle
pattern-printing
School Programming
pattern-printing
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Constructors in Java
Exceptions in Java
Python Exception Handling
Python Try Except
Ternary Operator in Python
How JVM Works - JVM Architecture?
Python program to add two numbers
Variables in Java
Data types in Java
Difference between Abstract Class and Interface in Java