Skip to main content

Print hexagonal star pattern

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

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




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