Posted on by Kalkicode
Code Pattern

Print hexagonal star pattern

The hexagonal star pattern is a geometric pattern consisting of hexagons formed by stars. It is an interesting pattern that can be created using loops and conditional statements in a programming language. In this article, we will discuss the problem statement, provide an explanation of the code, and analyze its time complexity.

Problem Statement

The problem is to write a program that prints a hexagonal star pattern based on a given size. The program should take an integer as input representing the size of the hexagon, and then display the hexagon pattern on the console.

For example, if the input size is 4, the program should print the following pattern:

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Code Explanation

The provided code is written in the C programming language. It consists of several functions to create the hexagonal pattern:

  • space(int size): This function prints spaces based on the given size.
  • print_symbol(int size): This function prints a symbol (in this case, an asterisk '*') based on the given size.
  • hexagonal_pattern(int size): This function generates the hexagonal pattern based on the given size.
  • main(): This is the entry point of the program where the hexagonal patterns of different sizes are printed.

The hexagonal_pattern function takes the size of the hexagon as input. It first checks if the size is less than 2 and returns if it is. Then it proceeds to print the top layers and bottom layers of the hexagon using loops and conditional statements.

Algorithm and Pseudocode

Here is the algorithm for generating the hexagonal pattern:

  1. Check if the given size is less than 2. If so, return.
  2. Calculate the midpoint of the size.
  3. Print the top layers of the hexagon:
    1. Iterate from 0 to size - 1:
    2. Print spaces based on size - i.
    3. If i is 0, print the entire row with symbols (print_symbol function).
    4. Otherwise, print an asterisk, spaces ((size - 1) * 2) + i * 2 - 1, and another asterisk.
  4. Print the bottom layers of the hexagon:
    1. Iterate from 1 to size - 1:
    2. Print spaces based on i + 1.
    3. If i + 1 is equal to size, print the entire row with symbols (print_symbol function).
    4. Otherwise, print an asterisk, spaces ((size - 1) * 4) - i * 2 - 1, and another asterisk.

Here is the corresponding pseudocode:

function space(size):
    for counter from 0 to size:
        print space

function print_symbol(size):
    for counter from 0 to size:
        print asterisk

function hexagonal_pattern(size):
    if size < 2:
        return
    mid = size / 2
    for i from 0 to size:
        space(size - i)
        if i == 0:
            print_symbol(size)
        else:
            print asterisk
            space(((size - 1) * 2) + i * 2 - 1)
            print asterisk
        print new line
    for i from 1 to size:
        space(i + 1)
        if i + 1 == size:
            print_symbol(size)
        else:
            print asterisk
            space(((size - 1) * 4) - i * 2 - 1)
            print asterisk
        print new line

main function:
    hexagonal_pattern(4)
    hexagonal_pattern(7)
    hexagonal_pattern(9)

Code Solution

Here given code implementation process.

//C Program 
//Print hexagonal star pattern
#include <stdio.h>

/*Include Space of given size*/
void space(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		//Add space
		printf(" ");
	}
}
void print_symbol(int size)
{
	int counter = 0;
	for (counter = 0; counter < size; counter++)
	{
		printf("* ");
	}
}
//Print hexagon of given side
void hexagonal_pattern(int size)
{
	if (size < 2)
	{
		return;
	}
	printf("\nSize : %d\n\n", size);
	int mid = size / 2;
	int i = 0;
	//Print top layers
	for (i = 0; i < size; ++i)
	{
		space(size - i);
		if (i == 0)
		{
			//first layer
			print_symbol(size);
		}
		else
		{
			printf("*");
			space(((size - 1) * 2) + i * 2 - 1);
			printf("*");
		}
		printf("\n");
	}
	//Print bottom layers
	for (i = 1; i < size; ++i)
	{
		space(i + 1);
		if (i + 1 == size)
		{
			//last layer
			print_symbol(size);
		}
		else
		{
			printf("*");
			space(((size - 1) * 4) - i * 2 - 1);
			printf("*");
		}
		printf("\n");
	}
}
int main()
{
	//Simple test
	hexagonal_pattern(4);
	hexagonal_pattern(7);
	hexagonal_pattern(9);
	return 0;
}

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
/*
  Java Program
  Print hexagonal star pattern
*/
class MyPattern
{
public void space(int size)
{
  int counter = 0;
  for (counter = 0; counter < size; counter++)
  {
    //Add space
    System.out.print(" ");
  }
}
public void print_symbol(int size)
{
  int counter = 0;
  for (counter = 0; counter < size; counter++)
  {
    System.out.print("* ");
  }
}
//Print hexagon of given side
public void hexagonal_pattern(int size)
{
  if (size < 2)
  {
    return;
  }
  System.out.print("\nSize : "+size+"\n\n");
  int mid = size / 2;
  int i = 0;
  //Print top layers
  for (i = 0; i < size; ++i)
  {
    space(size - i);
    if (i == 0)
    {
      //first layer
      print_symbol(size);
    }
    else
    {
      System.out.print("*");
      space(((size - 1) * 2) + i * 2 - 1);
      System.out.print("*");
    }
    System.out.print("\n");
  }
  //Print bottom layers
  for (i = 1; i < size; ++i)
  {
    space(i + 1);
    if (i + 1 == size)
    {
      //last layer
      print_symbol(size);
    }
    else
    {
      System.out.print("*");
      space(((size - 1) * 4) - i * 2 - 1);
      System.out.print("*");
    }
    System.out.print("\n");
  }
}
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
 
    //Simple test
    obj.hexagonal_pattern(4);
    obj.hexagonal_pattern(7);
    obj.hexagonal_pattern(9);
  }
}

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
/*
  C++ Program
  Print hexagonal star pattern
*/
#include<iostream>

using namespace std;
class MyPattern
{
	public: void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << " ";
		}
	}
	void print_symbol(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			cout << "* ";
		}
	}
	//Print hexagon of given side
	void hexagonal_pattern(int size)
	{
		if (size < 2)
		{
			return;
		}
		cout << "\nSize : " << size << "\n\n";
		int mid = size / 2;
		int i = 0;
		//Print top layers
		for (i = 0; i < size; ++i)
		{
			this->space(size - i);
			if (i == 0)
			{
				//first layer
				this->print_symbol(size);
			}
			else
			{
				cout << "*";
				this->space(((size - 1) * 2) + i * 2 - 1);
				cout << "*";
			}
			cout << "\n";
		}
		//Print bottom layers
		for (i = 1; i < size; ++i)
		{
			this->space(i + 1);
			if (i + 1 == size)
			{
				//last layer
				this->print_symbol(size);
			}
			else
			{
				cout << "*";
				this->space(((size - 1) * 4) - i * 2 - 1);
				cout << "*";
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern obj = MyPattern();
	//Simple test
	obj.hexagonal_pattern(4);
	obj.hexagonal_pattern(7);
	obj.hexagonal_pattern(9);
	return 0;
}

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
/*
  C# Program
  Print hexagonal star pattern
*/
using System;
class MyPattern
{
	public void space(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			Console.Write(" ");
		}
	}
	public void print_symbol(int size)
	{
		int counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			Console.Write("* ");
		}
	}
	//Print hexagon of given side
	public void hexagonal_pattern(int size)
	{
		if (size < 2)
		{
			return;
		}
		Console.Write("\nSize : " + size + "\n\n");
		int i = 0;
		//Print top layers
		for (i = 0; i < size; i++)
		{
			space(size - i);
			if (i == 0)
			{
				//first layer
				print_symbol(size);
			}
			else
			{
				Console.Write("*");
				space(((size - 1) * 2) + i * 2 - 1);
				Console.Write("*");
			}
			Console.Write("\n");
		}
		//Print bottom layers
		for (i = 1; i < size; i++)
		{
			space(i + 1);
			if (i + 1 == size)
			{
				//last layer
				print_symbol(size);
			}
			else
			{
				Console.Write("*");
				space(((size - 1) * 4) - i * 2 - 1);
				Console.Write("*");
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Simple test
		obj.hexagonal_pattern(4);
		obj.hexagonal_pattern(7);
		obj.hexagonal_pattern(9);
	}
}

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
<?php
/*
  Php Program
  Print hexagonal star pattern
*/
class MyPattern
{
	public	function space($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			//Add space
			echo(" ");
		}
	}
	public	function print_symbol($size)
	{
		$counter = 0;
		for ($counter = 0; $counter < $size; $counter++)
		{
			echo("* ");
		}
	}
	//Print hexagon of given side
	public	function hexagonal_pattern($size)
	{
		if ($size < 2)
		{
			return;
		}
		echo("\nSize : ". $size ."\n\n");
		$i = 0;
		//Print top layers
		for ($i = 0; $i < $size; ++$i)
		{
			$this->space($size - $i);
			if ($i == 0)
			{
				//first layer
				$this->print_symbol($size);
			}
			else
			{
				echo("*");
				$this->space((($size - 1) * 2) + $i * 2 - 1);
				echo("*");
			}
			echo("\n");
		}
		//Print bottom layers
		for ($i = 1; $i < $size; ++$i)
		{
			$this->space($i + 1);
			if ($i + 1 == $size)
			{
				//last layer
				$this->print_symbol($size);
			}
			else
			{
				echo("*");
				$this->space((($size - 1) * 4) - $i * 2 - 1);
				echo("*");
			}
			echo("\n");
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Simple test
	$obj->hexagonal_pattern(4);
	$obj->hexagonal_pattern(7);
	$obj->hexagonal_pattern(9);
}
main();

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
/*
  Node Js Program
  Print hexagonal star pattern
*/
class MyPattern
{
	space(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			//Add space
			process.stdout.write(" ");
		}
	}
	print_symbol(size)
	{
		var counter = 0;
		for (counter = 0; counter < size; counter++)
		{
			process.stdout.write("* ");
		}
	}
	//Print hexagon of given side
	hexagonal_pattern(size)
	{
		if (size < 2)
		{
			return;
		}
		process.stdout.write("\nSize : " + size + "\n\n");
		var i = 0;
		//Print top layers
		for (i = 0; i < size; ++i)
		{
			this.space(size - i);
			if (i == 0)
			{
				//first layer
				this.print_symbol(size);
			}
			else
			{
				process.stdout.write("*");
				this.space(((size - 1) * 2) + i * 2 - 1);
				process.stdout.write("*");
			}
			process.stdout.write("\n");
		}
		//Print bottom layers
		for (i = 1; i < size; ++i)
		{
			this.space(i + 1);
			if (i + 1 == size)
			{
				//last layer
				this.print_symbol(size);
			}
			else
			{
				process.stdout.write("*");
				this.space(((size - 1) * 4) - i * 2 - 1);
				process.stdout.write("*");
			}
			process.stdout.write("\n");
		}
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Simple test
	obj.hexagonal_pattern(4);
	obj.hexagonal_pattern(7);
	obj.hexagonal_pattern(9);
}
main();

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
#   Python 3 Program
#   Print hexagonal star pattern

class MyPattern :
	def space(self, size) :
		counter = 0
		counter = 0
		while (counter < size) :
			print(" ", end = "")
			counter += 1
		
	
	def print_symbol(self, size) :
		counter = 0
		counter = 0
		while (counter < size) :
			print("* ", end = "")
			counter += 1
		
	
	# Print hexagon of given side
	def hexagonal_pattern(self, size) :
		if (size < 2) :
			return
		
		print("\nSize : ", size ,"\n\n", end = "")
		i = 0
		# Print top layers
		i = 0
		while (i < size) :
			self.space(size - i)
			if (i == 0) :
				# first layer
				self.print_symbol(size)
			else :
				print("*", end = "")
				self.space(((size - 1) * 2) + i * 2 - 1)
				print("*", end = "")
			
			print("\n", end = "")
			i += 1
		
		# Print bottom layers
		i = 1
		while (i < size) :
			self.space(i + 1)
			if (i + 1 == size) :
				# last layer
				self.print_symbol(size)
			else :
				print("*", end = "")
				self.space(((size - 1) * 4) - i * 2 - 1)
				print("*", end = "")
			
			print("\n", end = "")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Simple test
	obj.hexagonal_pattern(4)
	obj.hexagonal_pattern(7)
	obj.hexagonal_pattern(9)


if __name__ == "__main__": main()

Output

Size :  4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size :  7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size :  9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
# Ruby Program
# Print hexagonal star pattern

class MyPattern

	def space(size)
	
		counter = 0
		counter = 0
		while (counter < size)
		
			print(" ")
			counter += 1
		end
	end
	def print_symbol(size)
	
		counter = 0
		counter = 0
		while (counter < size)
		
			print("* ")
			counter += 1
		end
	end
	# Print hexagon of given side
	def hexagonal_pattern(size)
	
		if (size < 2)
		
			return
		end
		print("\nSize : ", size ,"\n\n")
		i = 0
		# Print top layers
		i = 0
		while (i < size)
		
			self.space(size - i)
			if (i == 0)
			
				# first layer
				self.print_symbol(size)
			else
			
				print("*")
				self.space(((size - 1) * 2) + i * 2 - 1)
				print("*")
			end
			print("\n")
			i += 1
		end
		# Print bottom layers
		i = 1
		while (i < size)
		
			self.space(i + 1)
			if (i + 1 == size)
			
				# last layer
				self.print_symbol(size)
			else
			
				print("*")
				self.space(((size - 1) * 4) - i * 2 - 1)
				print("*")
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Simple test
	obj.hexagonal_pattern(4)
	obj.hexagonal_pattern(7)
	obj.hexagonal_pattern(9)
end
main()

Output

Size : 4

    * * * * 
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * * 

Size : 7

       * * * * * * * 
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * * 

Size : 9

         * * * * * * * * * 
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * * 
/*
  Scala Program
  Print hexagonal star pattern
*/
class MyPattern
{
	def space(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ");
			counter += 1;
		}
	}
	def print_symbol(size: Int): Unit = {
		var counter: Int = 0;
		while (counter < size)
		{
			print("* ");
			counter += 1;
		}
	}
	//Print hexagon of given side
	def hexagonal_pattern(size: Int): Unit = {
		if (size < 2)
		{
			return;
		}
		print("\nSize : " + size + "\n\n");
		var i: Int = 0;
		//Print top layers
		i = 0;
		while (i < size)
		{
			space(size - i);
			if (i == 0)
			{
				//first layer
				print_symbol(size);
			}
			else
			{
				print("*");
				space(((size - 1) * 2) + i * 2 - 1);
				print("*");
			}
			print("\n");
			i += 1;
		}
		//Print bottom layers
		i = 1;
		while (i < size)
		{
			space(i + 1);
			if (i + 1 == size)
			{
				//last layer
				print_symbol(size);
			}
			else
			{
				print("*");
				space(((size - 1) * 4) - i * 2 - 1);
				print("*");
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Simple test
		obj.hexagonal_pattern(4);
		obj.hexagonal_pattern(7);
		obj.hexagonal_pattern(9);
	}
}

Output

Size : 4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size : 7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size : 9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *
/*
  Swift Program
  Print hexagonal star pattern
*/
class MyPattern
{
	func space(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print(" ", terminator: "");
			counter += 1;
		}
	}
	func print_symbol(_ size: Int)
	{
		var counter: Int = 0;
		while (counter < size)
		{
			print("* ", terminator: "");
			counter += 1;
		}
	}
	//Print hexagon of given side
	func hexagonal_pattern(_ size: Int)
	{
		if (size < 2)
		{
			return;
		}
		print("\nSize : ", size ,"\n\n", terminator: "");
		var i: Int = 0;
		//Print top layers
		i = 0;
		while (i < size)
		{
			self.space(size - i);
			if (i == 0)
			{
				//first layer
				self.print_symbol(size);
			}
			else
			{
				print("*", terminator: "");
				self.space(((size - 1) * 2) + i * 2 - 1);
				print("*", terminator: "");
			}
			print("\n", terminator: "");
			i += 1;
		}
		//Print bottom layers
		i = 1;
		while (i < size)
		{
			self.space(i + 1);
			if (i + 1 == size)
			{
				//last layer
				self.print_symbol(size);
			}
			else
			{
				print("*", terminator: "");
				self.space(((size - 1) * 4) - i * 2 - 1);
				print("*", terminator: "");
			}
			print("\n", terminator: "");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Simple test
	obj.hexagonal_pattern(4);
	obj.hexagonal_pattern(7);
	obj.hexagonal_pattern(9);
}
main();

Output

Size :  4

    * * * *
   *       *
  *         *
 *           *
  *         *
   *       *
    * * * *

Size :  7

       * * * * * * *
      *             *
     *               *
    *                 *
   *                   *
  *                     *
 *                       *
  *                     *
   *                   *
    *                 *
     *               *
      *             *
       * * * * * * *

Size :  9

         * * * * * * * * *
        *                 *
       *                   *
      *                     *
     *                       *
    *                         *
   *                           *
  *                             *
 *                               *
  *                             *
   *                           *
    *                         *
     *                       *
      *                     *
       *                   *
        *                 *
         * * * * * * * * *

Time Complexity Analysis

The time complexity of the provided code depends on the size of the hexagon (input) and can be analyzed as follows:

  • The space function and the print_symbol function both have a time complexity of O(size) since they involve looping based on the given size.
  • The hexagonal_pattern function has two nested loops. The first loop iterates from 0 to size, and the second loop iterates from 1 to size. Therefore, the overall time complexity of the function is O(size^2).
  • The main function calls the hexagonal_pattern function multiple times, but each call is independent of the size. Hence, the time complexity of the main function can be considered as O(1).

Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment