Java Program to Print Circle Character Pattern

Program to Print Circle Character Pattern

In the previous article, we have discussed Java Program to Print Zig Zag Character Pattern

In this article we are going to see how to print the circle character pattern.

Example-1

When Size :    9

        E E E E E 
    C C C C C C C 
   B B B B B B B B 
A A A A A A A A A 
A A A A A A A A A 
A A A A A A A A A 
   B B B B B B B B 
    C C C C C C C 
        E E E E E

Now, let’s see the actual program to print it.

Are you wondering how to seek help from subject matter experts and learn the Java language? Go with these Basic Java Programming Examples and try to code all of them on your own then check with the exact code provided by expert programmers.

Approach:

  • Enter size and store it in an integer variable size.
  • Take outer for loop to iterate the rows.
  • Set the iterators using the nested if…else.
  • Use two for loops using the iterators to print space and characters.

Java Code to Print Circle Character Pattern

import java.util.Scanner;
class Main
{
  public static void main(String[] args) 
  {
     int r,c,k,iter_1,iter_2, size;
     //starting ASCII value taken 64
     int ascii=64;
    Scanner scan = new Scanner(System.in);
    
    //Taking size as input from user
    System.out.print("Size : ");
    size =scan.nextInt();
    
        //Outer loop
        for(r=1;r<=size;r++)
        {
          //Nested if..else to set the iterators value
          if(r==1||r==size){
            iter_2 = 4;
            iter_1 = size-iter_2;
          }
          else if(r==2||r==size-1){
              iter_2 = 2;
              iter_1 = size-iter_2;
          }
          else if(r==3||r==size-2){
              iter_2 = 1;
              iter_1 = size-iter_2;
          }
          else{
              iter_2 = 0;
              iter_1 = size;
          }
          //Iterators to print space and star
          for(c = 1; c <= iter_2; c++)
          {
              System.out.print(" ");
          }
          for(k = 1; k <= iter_1; k++)
          {
              System.out.print((char)(c+ascii)+" ");
          }
          System.out.println();
          //Prints a newline
        }
  }
}
Output:

Size :    9
  
        E E E E E 
    C C C C C C C 
   B B B B B B B B 
A A A A A A A A A 
A A A A A A A A A 
A A A A A A A A A 
   B B B B B B B B 
    C C C C C C C 
        E E E E E

Are you seeking professional help for coding in the Java programming language? The tutorial of Java Programming Examples for beginners and experts will strongly improve your coding skills then you can program for any logic in Java.

Related Java Character Pattern Programs: