Java Program to Print Hollow Circle Star Pattern

Program to Print Hollow Circle Star Pattern

In this article we are going to see how to print the hollow circle star pattern

Example-1

When radius value = 6
       *****   
    **         **
  **             **
 *                 *
*                   *
*                   *
*                   *
*                   *
*                   *
 *                 *
 **             **
  **          **
      *****
Example-2

When radius value = 9
          *******     
      ***          ***
    **                 **
   *                     *
  **                     **
  *                        *
**                         **
*                            *
*                            *
*                            *
*                            *
*                            *
**                         **
 *                         *
 **                     **
  *                     *
   **                **
    ***          ***
        *******

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Approach:

  • Enter the radius of the circle to print and store it in an integer variable radius.
  • Take first for loop to print all rows.
  • Take inner for loop to print column values and one to print empty spaces.
  • Then go on printing the star symbols according to the iteration.

JAVA Code:

Method-1 : Static Star Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        Scanner scan = new Scanner(System.in);
        System.out.print("Radius : ");
        //Taking radius of the circle as input from user
        int r, c, radius = scan.nextInt();
        double d;

        for( r=0;r<=2 * radius; r++)
        {//Outer loop
            for(c=0; c<=2*radius; c++)
            {//Inner loop
                d = Math.sqrt((r-radius) * (r-radius)
                + (c-radius) * (c-radius));//Logic Part

                if(d > radius-0.5 && d < radius+0.5)
                //Print star or whitespace
                    System.out.print("*");
                else
                    System.out.print(" ");
            }
            System.out.println();    
            //Prints a newline
        }
        
    }

}

Output

Radius : 6
    *****   
  **     **
 **       **
 *          *
*            *
*            *
*            *
*            *
*            *
 *          *
 **       **
  **     **
    *****

Method-2 : User Input Character

import java.util.Scanner;
class Main
{
    public static void main(String[] args)  
    {  
        int r, c, radius;
        Scanner scan = new Scanner(System.in);
        System.out.print("Radius : ");
        //Taking radius of the circle as input from user
        radius = scan.nextInt();
        double d;
        char l;
        System.out.print("Character : ");
        //Taking any random character as input from user
        l = scan.next().charAt(0);

        for( r=0;r<=2 * radius; r++)
        {//Outer loop
            for(c=0; c<=2*radius; c++)
            {//Inner loop
                d = Math.sqrt((r-radius) * (r-radius)
                + (c-radius) * (c-radius));//Logic Part

                if(d > radius-0.5 && d < radius+0.5)
                //Print star or whitespace
                    System.out.print(l);
                else
                    System.out.print(" ");
            }
            System.out.println();    
            //Prints a newline
        }
        
    }

}

Output

Radius : 6
Character : *
    *****   
  **     **
 **       **
 *           *
*             *
*             *
*             *
*             *
*             *
 *           *
 **        **
  **      **
    *****

C Code:

#include <stdio.h>
#include <math.h>
int main(int argc, char const *argv[])
{
    printf("Radius : ");
    //Taking radius as input from user
    int radius, r, c;
    float d;
    scanf("%d", &radius);

    for (r = 0; r <= 2 * radius; r++)
    { //Outer loop
        for (c = 0; c <= 2 * radius; c++)
        {                                                                        //Inner loop
            d = sqrt((r - radius) * (r - radius) + (c - radius) * (c - radius)); //Logic Part

            if (d > radius - 0.5 && d < radius + 0.5)
                //Print star or whitespace
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
        //Prints a newline
    }
    return 0;
}


Output

Radius : 6
    *****   
  **     **
 **       **
 *          *
*            *
*            *
*            *
*            *
*            * 
 *          *
 **        **
  **      **
    *****

C++ Code:

#include <iostream>
#include <math.h>
using namespace std;
int main(int argc, char const *argv[])
{
    cout << "Radius : ";
    //Taking radius as input from user
    int radius, r, c;
    cin >> radius;
    float d;

    for (r = 0; r <= 2 * radius; r++)
    {
        for (c = 0; c <= 2 * radius; c++)
        {
            d = sqrt((r - radius) * (r - radius) + (c - radius) * (c - radius)); //Logic Part
            //Logic Part
            if (d > radius - 0.5 && d < radius + 0.5)
                //Print star or whitespace
                cout << "*";
            else
                cout << " ";
        }
        //Prints a newline
        cout << endl;
    }
    return 0;
}

Output

Radius : 6
    *****   
  **     **
 **       **
 *          *
*            *
*            *
*            *
*            *
*            *
 *          *
 **        **
  **      **
    *****

Related Java Star Pattern Programs: