Print the Pentagonal pattern

Here given code implementation process.

//C Program 
//Print the Pentagonal pattern
#include <stdio.h>

#include <stdlib.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("* ");
	}
}
//Display pentagonal pattern of given side 
void print_pentagonal(int side)
{
	if (side < 0)
	{
		return;
	}
	printf("\n  Side : %d \n\n", side);
	int j = 2;
	for (int i = 0; i < side * 2 - 1; ++i)
	{
		if (i < side)
		{
			//print top half layers
			space(side * 2 - (i * 2));
			print_symbol(i * 2 + 1);
		}
		else
		{
			//print bottom half layers
			space(j + 1);
			print_symbol((side - 1) * 2 - (i - side));
			j++;
		}
		printf("\n");
	}
}
int main()
{
	//Test Cases
	print_pentagonal(4);
	print_pentagonal(7);
	print_pentagonal(9);
	print_pentagonal(3);
	return 0;
}

Output

  Side : 4

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

  Side : 7

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

  Side : 9

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

  Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
/*
  Java Program
  Print the Pentagonal 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("* ");
  }
}
//Display pentagonal pattern of given side 
public void print_pentagonal(int side)
{
  if (side < 0)
  {
    return;
  }
  System.out.print("\n Side : "+side+" \n\n" );
  int j = 2;
  for (int i = 0; i < side * 2 - 1; ++i)
  {
    if (i < side)
    {
      //print top half layers
      space(side * 2 - (i * 2));
      print_symbol(i * 2 + 1);
    }
    else
    {
      //print bottom half layers
      space(j + 1);
      print_symbol((side - 1) * 2 - (i - side));
      j++;
    }
    System.out.print("\n");
  }
}
  public static void main(String[] args)
  {
    MyPattern obj = new MyPattern();
    //Test Cases
    obj.print_pentagonal(4);
    obj.print_pentagonal(7);
    obj.print_pentagonal(9);
    obj.print_pentagonal(3);
  }
}

Output

 Side : 4

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

 Side : 7

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

 Side : 9

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

 Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
/*
  C++ Program
  Print the Pentagonal 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 << "* ";
		}
	}
	//Display pentagonal pattern of given side 
	void print_pentagonal(int side)
	{
		if (side < 0)
		{
			return;
		}
		cout << "\n Side : " << side << " \n\n";
		int j = 2;
		for (int i = 0; i < side * 2 - 1; ++i)
		{
			if (i < side)
			{
				//print top half layers
				this->space(side * 2 - (i * 2));
				this->print_symbol(i * 2 + 1);
			}
			else
			{
				//print bottom half layers
				this->space(j + 1);
				this->print_symbol((side - 1) * 2 - (i - side));
				j++;
			}
			cout << "\n";
		}
	}
};
int main()
{
	MyPattern obj = MyPattern();
	//Test Cases
	obj.print_pentagonal(4);
	obj.print_pentagonal(7);
	obj.print_pentagonal(9);
	obj.print_pentagonal(3);
	return 0;
}

Output

 Side : 4

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

 Side : 7

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

 Side : 9

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

 Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
/*
  C# Program
  Print the Pentagonal 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("* ");
		}
	}
	//Display pentagonal pattern of given side 
	public void print_pentagonal(int side)
	{
		if (side < 0)
		{
			return;
		}
		Console.Write("\n Side : " + side + " \n\n");
		int j = 2;
		for (int i = 0; i < side * 2 - 1; i++)
		{
			if (i < side)
			{
				//print top half layers
				space(side * 2 - (i * 2));
				print_symbol(i * 2 + 1);
			}
			else
			{
				//print bottom half layers
				space(j + 1);
				print_symbol((side - 1) * 2 - (i - side));
				j++;
			}
			Console.Write("\n");
		}
	}
	public static void Main(String[] args)
	{
		MyPattern obj = new MyPattern();
		//Test Cases
		obj.print_pentagonal(4);
		obj.print_pentagonal(7);
		obj.print_pentagonal(9);
		obj.print_pentagonal(3);
	}
}

Output

 Side : 4

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

 Side : 7

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

 Side : 9

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

 Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
<?php
/*
  Php Program
  Print the Pentagonal 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("* ");
		}
	}
	//Display pentagonal pattern of given side 
	public	function print_pentagonal($side)
	{
		if ($side < 0)
		{
			return;
		}
		echo("\n Side : ". $side ." \n\n");
		$j = 2;
		for ($i = 0; $i < $side * 2 - 1; ++$i)
		{
			if ($i < $side)
			{
				//print top half layers
				$this->space($side * 2 - ($i * 2));
				$this->print_symbol($i * 2 + 1);
			}
			else
			{
				//print bottom half layers
				$this->space($j + 1);
				$this->print_symbol(($side - 1) * 2 - ($i - $side));
				$j++;
			}
			echo("\n");
		}
	}
}

function main()
{
	$obj = new MyPattern();
	//Test Cases
	$obj->print_pentagonal(4);
	$obj->print_pentagonal(7);
	$obj->print_pentagonal(9);
	$obj->print_pentagonal(3);
}
main();

Output

 Side : 4

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

 Side : 7

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

 Side : 9

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

 Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
/*
  Node Js Program
  Print the Pentagonal 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("* ");
		}
	}
	//Display pentagonal pattern of given side 
	print_pentagonal(side)
	{
		if (side < 0)
		{
			return;
		}
		process.stdout.write("\n Side : " + side + " \n\n");
		var j = 2;
		for (var i = 0; i < side * 2 - 1; ++i)
		{
			if (i < side)
			{
				//print top half layers
				this.space(side * 2 - (i * 2));
				this.print_symbol(i * 2 + 1);
			}
			else
			{
				//print bottom half layers
				this.space(j + 1);
				this.print_symbol((side - 1) * 2 - (i - side));
				j++;
			}
			process.stdout.write("\n");
		}
	}
}

function main(args)
{
	var obj = new MyPattern();
	//Test Cases
	obj.print_pentagonal(4);
	obj.print_pentagonal(7);
	obj.print_pentagonal(9);
	obj.print_pentagonal(3);
}
main();

Output

 Side : 4

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

 Side : 7

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

 Side : 9

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

 Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
#   Python 3 Program
#   Print the Pentagonal pattern

class MyPattern :
	def space(self, size) :
		counter = 0
		while (counter < size) :
			print( end = " ")
			counter += 1
		
	
	def print_symbol(self, size) :
		counter = 0
		while (counter < size) :
			print(end = "* ")
			counter += 1
		
	
	# Display pentagonal pattern of given side 
	def print_pentagonal(self, side) :
		if (side < 0) :
			return
		
		print("\n Side : ", side ," \n")
		j = 2
		i = 0
		while (i < side * 2 - 1) :
			if (i < side) :
				# print top half layers
				self.space(side * 2 - (i * 2))
				self.print_symbol(i * 2 + 1)
			else :
				# print bottom half layers
				self.space(j + 1)
				self.print_symbol((side - 1) * 2 - (i - side))
				j += 1
			
			print(end = "\n")
			i += 1
		
	

def main() :
	obj = MyPattern()
	# Test Cases
	obj.print_pentagonal(4)
	obj.print_pentagonal(7)
	obj.print_pentagonal(9)
	obj.print_pentagonal(3)


if __name__ == "__main__": main()

Output

 Side :  4

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

 Side :  7

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

 Side :  9

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

 Side :  3

      *
    * * *
  * * * * *
   * * * *
    * * *
#   Ruby Program
#   Print the Pentagonal pattern

class MyPattern

	def space(size)
	
		counter = 0
		while (counter < size)
		
			print(" ")
			counter += 1
		end
	end
	def print_symbol(size)
	
		counter = 0
		while (counter < size)
		
			print("* ")
			counter += 1
		end
	end
	# Display pentagonal pattern of given side 
	def print_pentagonal(side)
	
		if (side < 0)
		
			return
		end
		print("\n Side : ", side ," \n\n")
		j = 2
		i = 0
		while (i < side * 2 - 1)
		
			if (i < side)
			
				# print top half layers
				self.space(side * 2 - (i * 2))
				self.print_symbol(i * 2 + 1)
			else
			
				# print bottom half layers
				self.space(j + 1)
				self.print_symbol((side - 1) * 2 - (i - side))
				j += 1
			end
			print("\n")
			i += 1
		end
	end
end
def main()

	obj = MyPattern.new()
	# Test Cases
	obj.print_pentagonal(4)
	obj.print_pentagonal(7)
	obj.print_pentagonal(9)
	obj.print_pentagonal(3)
end
main()

Output

 Side : 4 

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

 Side : 7 

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

 Side : 9 

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

 Side : 3 

      * 
    * * * 
  * * * * * 
   * * * * 
    * * * 
/*
  Scala Program
  Print the Pentagonal 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;
		}
	}
	//Display pentagonal pattern of given side 
	def print_pentagonal(side: Int): Unit = {
		if (side < 0)
		{
			return;
		}
		print("\n Side : " + side + " \n\n");
		var j: Int = 2;
		var i: Int = 0;
		while (i < side * 2 - 1)
		{
			if (i < side)
			{
				//print top half layers
				space(side * 2 - (i * 2));
				print_symbol(i * 2 + 1);
			}
			else
			{
				//print bottom half layers
				space(j + 1);
				print_symbol((side - 1) * 2 - (i - side));
				j += 1;
			}
			print("\n");
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyPattern = new MyPattern();
		//Test Cases
		obj.print_pentagonal(4);
		obj.print_pentagonal(7);
		obj.print_pentagonal(9);
		obj.print_pentagonal(3);
	}
}

Output

 Side : 4

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

 Side : 7

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

 Side : 9

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

 Side : 3

      *
    * * *
  * * * * *
   * * * *
    * * *
/*
  Swift Program
  Print the Pentagonal 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;
		}
	}
	//Display pentagonal pattern of given side 
	func print_pentagonal(_ side: Int)
	{
		if (side < 0)
		{
			return;
		}
		print("\n Side : ", side ," \n");
		var j: Int = 2;
		var i: Int = 0;
		while (i < side * 2 - 1)
		{
			if (i < side)
			{
				//print top half layers
				self.space(side * 2 - (i * 2));
				self.print_symbol(i * 2 + 1);
			}
			else
			{
				//print bottom half layers
				self.space(j + 1);
				self.print_symbol((side - 1) * 2 - (i - side));
				j += 1;
			}
			print(terminator: "\n");
			i += 1;
		}
	}
}
func main()
{
	let obj: MyPattern = MyPattern();
	//Test Cases
	obj.print_pentagonal(4);
	obj.print_pentagonal(7);
	obj.print_pentagonal(9);
	obj.print_pentagonal(3);
}
main();

Output

 Side :  4

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

 Side :  7

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

 Side :  9

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

 Side :  3

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


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







© 2021, kalkicode.com, All rights reserved