Posted on by Kalkicode
Code Pattern

# Print the hollow Pentagonal pattern

The given problem is to print a hollow pentagonal pattern of a specified size. The pattern consists of a series of asterisks (*) arranged in the shape of a pentagon, with spaces in between. The size of the pentagon is determined by the input provided. For example, if the size is 4, the pattern would look like this:

```        *
*   *
*       *
*           *
*         *
*       *
* * * *
```

The pattern starts with a single asterisk at the top and gradually expands outward to form the pentagon shape. The pattern then hollows out in the center, leaving spaces between the asterisks. The bottom part of the pattern is a reflection of the top part, resulting in a symmetric pentagon. To solve this problem, we can use a combination of loops and conditional statements. Here is an explanation of the algorithm and pseudocode:

## Algorithm:

1. Create a class named MyPattern.
2. Define a method named space that takes an integer size as a parameter.
3. Initialize a counter variable to 0.
4. Start a loop that iterates until the counter is less than the size.
5. Inside the loop, print a space character.
6. Define another method named print_symbol that also takes an integer size as a parameter.
7. Initialize the counter variable to 0.
8. Start a loop that iterates until the counter is less than the size.
9. Inside the loop, print an asterisk followed by a space character.
10. Define a method named print_pentagonal that takes an integer size as a parameter.
11. Check if the size is less than 0. If true, return from the method.
12. Print the size of the pentagon.
13. Initialize a variable j to 2.
14. Start a loop that iterates from 0 to (size - 1) * 2.
15. Inside the loop, check if the counter is less than size.
16. If true, it means we are in the top half of the pattern.
17. Call the space method with the argument size * 2 - (i * 2).
18. Print an asterisk.
19. Call the space method with the argument (i * 2) * 2 - 1.
20. Check if the counter is greater than 0.
21. If true, print an asterisk.
22. If false, it means we are in the bottom half of the pattern.
23. Call the space method with the argument j + 1.
24. Check if the counter is equal to (size - 1) * 2.
25. If true, call the print_symbol method with the argument (size - 1) * 2 - (i - size).
26. If false, print an asterisk followed by a space character.
27. Call the space method with the argument (size - 1) * 4 - j * 2.
28. Print an asterisk.
29. Increase the value of j by 1.
30. Print a new line character.
31. Create an instance of the MyPattern class.
32. Call the print_pentagonal method with different test case sizes.

## Pseudocode

``````class MyPattern
method space(size)
counter = 0
loop until counter < size
print " "
increment counter

method print_symbol(size)
counter = 0
loop until counter < size
print "* "
increment counter

method print_pentagonal(size)
if size < 0
return
print "Side: " + size + "\n\n"
j = 2
loop i from 0 to (size - 1) * 2
if i < size
space(size * 2 - (i * 2))
print "*"
space((i * 2) * 2 - 1)
if i > 0
print "*"
else
space(j + 1)
if i == (size - 1) * 2
print_symbol((size - 1) * 2 - (i - size))
else
print "* "
space((size - 1) * 4 - j * 2)
print "*"
increment j
print new line

method main(args)
obj = new MyPattern()
obj.print_pentagonal(4)
obj.print_pentagonal(7)
obj.print_pentagonal(9)
obj.print_pentagonal(3)
``````

Here given code implementation process.

``````/*
Java Program
Print the hollow Pentagonal pattern
*/
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
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 size)
{
if (size < 0)
{
return;
}
System.out.print("\n Side : " + size + " \n\n");
int j = 2;
for (int i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size)
{
//print top half layers
space(size * 2 - (i * 2));
System.out.print("*");
space((i * 2) * 2 - 1);
if (i > 0)
{
System.out.print("*");
}
}
else
{
//print bottom half layers
space(j + 1);
if (i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
System.out.print("* ");
space((size - 1) * 4 - j * 2);
System.out.print("*");
}
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 hollow 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++)
{
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 size)
{
if (size < 0)
{
return;
}
printf("\n  Side : %d \n\n", size);
int j = 2;
for (int i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size)
{
//print top half layers
space(size * 2 - (i * 2));
printf("*");
space((i * 2) * 2 - 1);
if (i > 0)
{
printf("*");
}
}
else
{
//print bottom half layers
space(j + 1);
if (i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
printf("* ");
space((size - 1) * 4 - j * 2);
printf("*");
}
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

*
*   *
*       *
*     *
* * *``````
``````/*
C++ Program
Print the hollow 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 size)
{
if (size < 0)
{
return;
}
cout << "\n Side : " << size << " \n\n";
int j = 2;
for (int i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size)
{
//print top half layers
this->space(size * 2 - (i * 2));
cout << "*";
this->space((i * 2) * 2 - 1);
if (i > 0)
{
cout << "*";
}
}
else
{
//print bottom half layers
this->space(j + 1);
if (i == (size - 1) * 2)
{
this->print_symbol((size - 1) * 2 - (i - size));
}
else
{
cout << "* ";
this->space((size - 1) * 4 - j * 2);
cout << "*";
}
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 hollow Pentagonal pattern
*/
using System;
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
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 size)
{
if (size < 0)
{
return;
}
Console.Write("\n Side : " + size + " \n\n");
int j = 2;
for (int i = 0; i <= (size - 1) * 2; i++)
{
if (i < size)
{
//print top half layers
space(size * 2 - (i * 2));
Console.Write("*");
space((i * 2) * 2 - 1);
if (i > 0)
{
Console.Write("*");
}
}
else
{
//print bottom half layers
space(j + 1);
if (i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
Console.Write("* ");
space((size - 1) * 4 - j * 2);
Console.Write("*");
}
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 hollow Pentagonal pattern
*/
class MyPattern
{
public  function space(\$size)
{
\$counter = 0;
for (\$counter = 0; \$counter < \$size; \$counter++)
{
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(\$size)
{
if (\$size < 0)
{
return;
}
echo("\n Side : ". \$size ." \n\n");
\$j = 2;
for (\$i = 0; \$i <= (\$size - 1) * 2; ++\$i)
{
if (\$i < \$size)
{
//print top half layers
\$this->space(\$size * 2 - (\$i * 2));
echo("*");
\$this->space((\$i * 2) * 2 - 1);
if (\$i > 0)
{
echo("*");
}
}
else
{
//print bottom half layers
\$this->space(\$j + 1);
if (\$i == (\$size - 1) * 2)
{
\$this->print_symbol((\$size - 1) * 2 - (\$i - \$size));
}
else
{
echo("* ");
\$this->space((\$size - 1) * 4 - \$j * 2);
echo("*");
}
\$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 hollow Pentagonal pattern
*/
class MyPattern
{
space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
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(size)
{
if (size < 0)
{
return;
}
process.stdout.write("\n Side : " + size + " \n\n");
var j = 2;
for (var i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size)
{
//print top half layers
this.space(size * 2 - (i * 2));
process.stdout.write("*");
this.space((i * 2) * 2 - 1);
if (i > 0)
{
process.stdout.write("*");
}
}
else
{
//print bottom half layers
this.space(j + 1);
if (i == (size - 1) * 2)
{
this.print_symbol((size - 1) * 2 - (i - size));
}
else
{
process.stdout.write("* ");
this.space((size - 1) * 4 - j * 2);
process.stdout.write("*");
}
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 hollow 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, size) :
if (size < 0) :
return

print("\n Side : ", size ," \n")
j = 2
i = 0
while (i <= (size - 1) * 2) :
if (i < size) :
# print top half layers
self.space(size * 2 - (i * 2))
print("*", end = "")
self.space((i * 2) * 2 - 1)
if (i > 0) :
print("*", end = "")

else :
# print bottom half layers
self.space(j + 1)
if (i == (size - 1) * 2) :
self.print_symbol((size - 1) * 2 - (i - size))
else :
print("* ", end = "")
self.space((size - 1) * 4 - j * 2)
print(end = "*")

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 hollow 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(size)

if (size < 0)

return
end
print("\n Side : ", size ," \n\n")
j = 2
i = 0
while (i <= (size - 1) * 2)

if (i < size)

# print top half layers
self.space(size * 2 - (i * 2))
print("*")
self.space((i * 2) * 2 - 1)
if (i > 0)

print("*")
end
else

# print bottom half layers
self.space(j + 1)
if (i == (size - 1) * 2)

self.print_symbol((size - 1) * 2 - (i - size))
else

print("* ")
self.space((size - 1) * 4 - j * 2)
print("*")
end
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 hollow 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(size: Int): Unit = {
if (size < 0)
{
return;
}
print("\n Side : " + size + " \n\n");
var j: Int = 2;
var i: Int = 0;
while (i <= (size - 1) * 2)
{
if (i < size)
{
//print top half layers
space(size * 2 - (i * 2));
print("*");
space((i * 2) * 2 - 1);
if (i > 0)
{
print("*");
}
}
else
{
//print bottom half layers
space(j + 1);
if (i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
print("* ");
space((size - 1) * 4 - j * 2);
print("*");
}
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 hollow 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(_ size: Int)
{
if (size < 0)
{
return;
}
print("\n Side : ", size ," \n\n", terminator: "");
var j: Int = 2;
var i: Int = 0;
while (i <= (size - 1) * 2)
{
if (i < size)
{
//print top half layers
self.space(size * 2 - (i * 2));
print("*", terminator: "");
self.space((i * 2) * 2 - 1);
if (i > 0)
{
print("*", terminator: "");
}
}
else
{
//print bottom half layers
self.space(j + 1);
if (i == (size - 1) * 2)
{
self.print_symbol((size - 1) * 2 - (i - size));
}
else
{
print("* ", terminator: "");
self.space((size - 1) * 4 - j * 2);
print("*", terminator: "");
}
j += 1;
}
print("\n", terminator: "");
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

*
*   *
*       *
*     *
* * *``````

The time complexity of this code is O(n^2) because there are nested loops involved. The outer loop runs (size - 1) * 2 times, and the inner loop runs size times. As a result, the overall time complexity is quadratic.

The output of the code matches the expected pattern for different input sizes. The pentagonal pattern is displayed correctly, with the top and bottom halves forming a symmetrical shape. The spaces and asterisks are positioned correctly to create the hollow effect in the pattern.

## Print the triangles of the pentagon

``````//C Program
//Print the triangles of the pentagon
#include <stdio.h>

/*Include Space of given size*/
void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
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 size)
{
if (size < 0)
{
return;
}
printf("\n  Side : %d \n\n", size);
int j = 2;
for (int i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size - 1)
{
space(size * 2 - (i * 2));
printf("*");
if (i > 0)
{
space(i * 2 - 1);
printf("*");
space((i * 2) - 1);
printf("*");
}
}
else if (i == size - 1)
{
space(j);
print_symbol((size - 1) * 2 - (i - size));
}
else
{
space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
//Display the all bottom sides
printf("* ");
space((size - 1) * 2 - (j * 2));
printf("*");
space((j - 1) * 2 - 1);
printf("*");
space((size - 1) * 2 - (j * 2) + 1);
printf("*");
}
j++;
}
printf("\n");
}
}
int main()
{
//Test Cases
print_pentagonal(4);
print_pentagonal(7);
print_pentagonal(9);
print_pentagonal(8);
return 0;
}``````

#### Output

``````  Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````/*
Java Program
Print the triangles of the pentagon
*/
class MyPattern
{
public void space(int size)
{
int counter = 0;
for (counter = 0; counter < size; counter++)
{
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 size)
{
if (size < 0)
{
return;
}
System.out.print("\n Side : " + size + " \n\n");
int j = 2;
for (int i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size - 1)
{
space(size * 2 - (i * 2));
System.out.print("*");
if (i > 0)
{
space(i * 2 - 1);
System.out.print("*");
space((i * 2) - 1);
System.out.print("*");
}
}
else if (i == size - 1)
{
space(j);
print_symbol((size - 1) * 2 - (i - size));
}
else
{
space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
//Display the all bottom sides
System.out.print("* ");
space((size - 1) * 2 - (j * 2));
System.out.print("*");
space((j - 1) * 2 - 1);
System.out.print("*");
space((size - 1) * 2 - (j * 2) + 1);
System.out.print("*");
}
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(8);
}
}``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````/*
C++ Program
Print the triangles of the pentagon
*/
#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 size)
{
if (size < 0)
{
return;
}
cout << "\n Side : " << size << " \n\n";
int j = 2;
for (int i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size - 1)
{
this->space(size * 2 - (i * 2));
cout << "*";
if (i > 0)
{
this->space(i * 2 - 1);
cout << "*";
this->space((i * 2) - 1);
cout << "*";
}
}
else if (i == size - 1)
{
this->space(j);
this->print_symbol((size - 1) * 2 - (i - size));
}
else
{
this->space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
this->print_symbol((size - 1) * 2 - (i - size));
}
else
{
cout << "* ";
this->space((size - 1) * 2 - (j * 2));
cout << "*";
this->space((j - 1) * 2 - 1);
cout << "*";
this->space((size - 1) * 2 - (j * 2) + 1);
cout << "*";
}
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(8);
return 0;
}``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````<?php
/*
Php Program
Print the triangles of the pentagon
*/
class MyPattern
{
public  function space(\$size)
{
\$counter = 0;
for (\$counter = 0; \$counter < \$size; \$counter++)
{
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(\$size)
{
if (\$size < 0)
{
return;
}
echo("\n Side : ". \$size ." \n\n");
\$j = 2;
for (\$i = 0; \$i <= (\$size - 1) * 2; ++\$i)
{
if (\$i < \$size - 1)
{
\$this->space(\$size * 2 - (\$i * 2));
echo("*");
if (\$i > 0)
{
\$this->space(\$i * 2 - 1);
echo("*");
\$this->space((\$i * 2) - 1);
echo("*");
}
}
else if (\$i == \$size - 1)
{
\$this->space(\$j);
\$this->print_symbol((\$size - 1) * 2 - (\$i - \$size));
}
else
{
\$this->space(\$j + 1);
if (\$i == \$size - 1 || \$i == (\$size - 1) * 2)
{
\$this->print_symbol((\$size - 1) * 2 - (\$i - \$size));
}
else
{
//Display the all bottom sides
echo("* ");
\$this->space((\$size - 1) * 2 - (\$j * 2));
echo("*");
\$this->space((\$j - 1) * 2 - 1);
echo("*");
\$this->space((\$size - 1) * 2 - (\$j * 2) + 1);
echo("*");
}
\$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(8);
}
main();``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````/*
Node Js Program
Print the triangles of the pentagon
*/
class MyPattern
{
space(size)
{
var counter = 0;
for (counter = 0; counter < size; counter++)
{
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(size)
{
if (size < 0)
{
return;
}
process.stdout.write("\n Side : " + size + " \n\n");
var j = 2;
for (var i = 0; i <= (size - 1) * 2; ++i)
{
if (i < size - 1)
{
this.space(size * 2 - (i * 2));
process.stdout.write("*");
if (i > 0)
{
this.space(i * 2 - 1);
process.stdout.write("*");
this.space((i * 2) - 1);
process.stdout.write("*");
}
}
else if (i == size - 1)
{
this.space(j);
this.print_symbol((size - 1) * 2 - (i - size));
}
else
{
this.space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
this.print_symbol((size - 1) * 2 - (i - size));
}
else
{
//Display the all bottom sides
process.stdout.write("* ");
this.space((size - 1) * 2 - (j * 2));
process.stdout.write("*");
this.space((j - 1) * 2 - 1);
process.stdout.write("*");
this.space((size - 1) * 2 - (j * 2) + 1);
process.stdout.write("*");
}
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(8);
}
main();``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````#   Python 3 Program
#   Print the triangles of the pentagon

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, size) :
if (size < 0) :
return

print("\n Side : ", size ," \n\n", end = "")
j = 2
i = 0
while (i <= (size - 1) * 2) :
if (i < size - 1) :
self.space(size * 2 - (i * 2))
print("*", end = "")
if (i > 0) :
self.space(i * 2 - 1)
print("*", end = "")
self.space((i * 2) - 1)
print("*", end = "")

elif(i == size - 1) :
self.space(j)
self.print_symbol((size - 1) * 2 - (i - size))
else :
self.space(j + 1)
if (i == size - 1 or i == (size - 1) * 2) :
self.print_symbol((size - 1) * 2 - (i - size))
else :
print("* ", end = "")
self.space((size - 1) * 2 - (j * 2))
print("*", end = "")
self.space((j - 1) * 2 - 1)
print("*", end = "")
self.space((size - 1) * 2 - (j * 2) + 1)
print("*", end = "")

j += 1

print("\n", end = "")
i += 1

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

if __name__ == "__main__": main()``````

#### Output

`````` Side :  4

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

Side :  7

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

Side :  9

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

Side :  8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````#   Ruby Program
#   Print the triangles of the pentagon

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(size)

if (size < 0)

return
end
print("\n Side : ", size ," \n\n")
j = 2
i = 0
while (i <= (size - 1) * 2)

if (i < size - 1)

self.space(size * 2 - (i * 2))
print("*")
if (i > 0)

self.space(i * 2 - 1)
print("*")
self.space((i * 2) - 1)
print("*")
end
elsif(i == size - 1)

self.space(j)
self.print_symbol((size - 1) * 2 - (i - size))
else

self.space(j + 1)
if (i == size - 1 || i == (size - 1) * 2)

self.print_symbol((size - 1) * 2 - (i - size))
else

print("* ")
self.space((size - 1) * 2 - (j * 2))
print("*")
self.space((j - 1) * 2 - 1)
print("*")
self.space((size - 1) * 2 - (j * 2) + 1)
print("*")
end
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(8)
end
main()``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *
``````
``````/*
Scala Program
Print the triangles of the pentagon
*/
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(size: Int): Unit = {
if (size < 0)
{
return;
}
print("\n Side : " + size + " \n\n");
var j: Int = 2;
var i: Int = 0;
while (i <= (size - 1) * 2)
{
if (i < size - 1)
{
space(size * 2 - (i * 2));
print("*");
if (i > 0)
{
space(i * 2 - 1);
print("*");
space((i * 2) - 1);
print("*");
}
}
else if (i == size - 1)
{
space(j);
print_symbol((size - 1) * 2 - (i - size));
}
else
{
space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
print("* ");
space((size - 1) * 2 - (j * 2));
print("*");
space((j - 1) * 2 - 1);
print("*");
space((size - 1) * 2 - (j * 2) + 1);
print("*");
}
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(8);
}
}``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````/*
Swift Program
Print the triangles of the pentagon
*/
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(_ size: Int)
{
if (size < 0)
{
return;
}
print("\n Side : ", size ," \n\n", terminator: "");
var j: Int = 2;
var i: Int = 0;
while (i <= (size - 1) * 2)
{
if (i < size - 1)
{
self.space(size * 2 - (i * 2));
print("*", terminator: "");
if (i > 0)
{
self.space(i * 2 - 1);
print("*", terminator: "");
self.space((i * 2) - 1);
print("*", terminator: "");
}
}
else if (i == size - 1)
{
self.space(j);
self.print_symbol((size - 1) * 2 - (i - size));
}
else
{
self.space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
self.print_symbol((size - 1) * 2 - (i - size));
}
else
{
print("* ", terminator: "");
self.space((size - 1) * 2 - (j * 2));
print("*", terminator: "");
self.space((j - 1) * 2 - 1);
print("*", terminator: "");
self.space((size - 1) * 2 - (j * 2) + 1);
print("*", terminator: "");
}
j += 1;
}
print("\n", terminator: "");
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(8);
}
main();``````

#### Output

`````` Side :  4

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

Side :  7

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

Side :  9

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

Side :  8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````
``````/*
C# Program
Print the triangles of the pentagon
*/
using System;
class MyPattern
{
public void space(int size)
{
int counter = 0;
while (counter < size)
{
Console.Write(" ");
counter++;
}
}
public void print_symbol(int size)
{
int counter = 0;
while (counter < size)
{
Console.Write("* ");
counter++;
}
}
//Display pentagonal pattern of given side
public void print_pentagonal(int size)
{
if (size < 0)
{
return;
}
Console.Write("\n Side : " + size + " \n\n");
int j = 2;
int i = 0;
while (i <= (size - 1) * 2)
{
if (i < size - 1)
{
space(size * 2 - (i * 2));
Console.Write("*");
if (i > 0)
{
space(i * 2 - 1);
Console.Write("*");
space((i * 2) - 1);
Console.Write("*");
}
}
else if (i == size - 1)
{
space(j);
print_symbol((size - 1) * 2 - (i - size));
}
else
{
space(j + 1);
if (i == size - 1 || i == (size - 1) * 2)
{
print_symbol((size - 1) * 2 - (i - size));
}
else
{
Console.Write("* ");
space((size - 1) * 2 - (j * 2));
Console.Write("*");
space((j - 1) * 2 - 1);
Console.Write("*");
space((size - 1) * 2 - (j * 2) + 1);
Console.Write("*");
}
j++;
}
Console.Write("\n");
i++;
}
}
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(8);
}
}``````

#### Output

`````` Side : 4

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

Side : 7

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

Side : 9

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

Side : 8

*
* * *
*   *   *
*     *     *
*       *       *
*         *         *
*           *           *
* * * * * * * * * * * * * * *
*           * *           *
*         *   *         *
*       *     *       *
*     *       *     *
*   *         *   *
* *           * *
* * * * * * * *``````

## 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.

Categories
Relative Post