Java Program to Print Hexagonal Character Pattern

Print Hexagonal Character Pattern

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

In this article we will see how to print hexagonal character pattern. Hexagon pattern program in java, hexagon pattern in java, character pattern programs in java, char pattern in java, character pattern in java, ascii hexagon, hexagonal pattern programming, hexagon ascii, java print char, print character pattern in java are discussed below.

Example-1
When each side of hexagon contains 10 characters then
         J--------------------------
        I K
       H  L
      G    M
     F       N                                                       UPPER PART
    E         O
   D           P
  C             Q
 B               R
A                 S -------------------
A                 S
A                 S
A                 S
A                 S                                                   MIDDLE PART
A                 S
A                 S
A                 S
A                 S
A                 S--------------------
 B               R
  C             Q
   D           P
    E         O
     F       N                                                       LOWER PART
      G    M
       H  L
         I K
          J---------------------------

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

The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.

Approach :

  1. We have divided the program into three parts upper part, middle part and lower part.
  2. Then we will print each part separately.
  3. Actually each row first value is character  ‘A’ as we have taken starting ASCII value 65. And printing in a row takes place like character A B C D E F …….
  4. But based on the condition in each row at respective position respective character will be printed and other places will be printed with spaces.

Java Code to Print Hexagonal Character Pattern

import java.util.*;
class Main
{
    public static void main (String[] args)
    {
        Scanner sc=new Scanner(System.in);
        
        // Starting ASCII value taken 65 
        int asciiAlpha = 65;
        
        // Asks for user input to enter any number
        // Where that number of stars will be printed 
        // in a side of hexagonal pattern
        System.out.print("Enter the number:");
        int side = sc.nextInt();
        
        int len = 2 * side - 1;
     
        // Printing upper part
        for (int i = 0; i < side; i++)
        {
            int element = i + side;
     
            // prints the star and spaces
            for (int k = 0; k < element; k++)
            {
                // printing the character
                if ((k == side + i - 1) || (k == side - i - 1))
                    System.out.print((char)(k + asciiAlpha));
                // printing the space
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
     
        // Printing middle part
        //  Two vertical lines will be printed with middle space
        for (int i = 0; i < side - 2; i++)
        {
            for (int j = 0; j < len; j++)
            {
                // printing character
                if (j == 0 || j == len - 1)
                    System.out.print((char)(j + asciiAlpha));
                // printing space
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
     
       // Printing lower part
        int r = side - 1;
        for (int i = r; i >= 0; i--)
        {
            int elem = i + side;
            for (int k = 0; k < elem; k++)
            {
                // printing the character
                if ((k == side + i - 1) || (k == side - i - 1))
                    System.out.print((char)(k + asciiAlpha));
                // printing the space
                else
                    System.out.print(" ");
            }
            System.out.print("\n");
        }
    }
  
}
Output:

Enter the number:   10      
         J
        I K
       H  L
      G    M
     F       N
    E         O
   D           P
  C             Q
 B               R
A                 S
A                 S
A                 S
A                 S
A                 S
A                 S
A                 S
A                 S
A                 S
A                 S
 B               R
  C             Q
   D            P
    E         O
     F       N
      G    M
       H   L
         I K
          J

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.

Also Refer: Java Program to Print Stair Case Star Pattern

Try these:

  1. How to print a hexagon in java?
  2. How to make a hexagon with asterisks in java?

Related Java Character Pattern Programs: