Print the Pentagonal pattern
In this article, we will explore the concept of printing a pentagonal pattern using a C program. The program will take a side length as input and generate a pentagonal pattern based on that input.
Problem Statement
The problem is to write a program that prints a pentagonal pattern based on the given side length. The pattern consists of asterisks (*) arranged in a specific way to form a pentagon shape. The pattern should be printed in the console output.
For example, let's say we have a side length of 4. The pentagonal pattern would look like this:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
The number of asterisks (*) in each row follows a specific pattern. In the top half of the pentagon, the number of asterisks increases by two for each row. In the bottom half, the number of asterisks decreases by two for each row.
Algorithm
To solve this problem, we can use the following algorithm:
- Define a function named
space
that takes a size parameter and prints the specified number of spaces. - Define a function named
print_symbol
that takes a size parameter and prints the specified number of asterisks (*) followed by a space. - Define a function named
print_pentagonal
that takes a side parameter and prints the pentagonal pattern. - Inside the
print_pentagonal
function, check if the side parameter is less than zero. If it is, return from the function. - Print a header indicating the current side length.
- Initialize a variable
j
to 2. - Use a loop to iterate over the rows of the pentagonal pattern.
- Inside the loop, use a conditional statement to determine whether to print the top half or bottom half of the pattern.
- If the current row is in the top half, call the
space
function to print the appropriate number of spaces, followed by theprint_symbol
function to print the appropriate number of asterisks. - If the current row is in the bottom half, adjust the spacing and number of asterisks accordingly and call the
space
andprint_symbol
functions. - Print a new line after each row.
- Call the
print_pentagonal
function with different side lengths to generate the desired patterns.
Pseudocode
function space(size):
for counter from 0 to size:
print " "
function print_symbol(size):
for counter from 0 to size:
print "* "
function print_pentagonal(side):
if side < 0:
return
print "Side: side"
j = 2
for i from 0 to side * 2 - 1:
if i < side:
space(side * 2 - (i * 2))
print_symbol(i * 2 + 1)
else:
space(j + 1)
print_symbol((side - 1) * 2 - (i - side))
j++
print new line
print_pentagonal(4)
print_pentagonal(7)
print_pentagonal(9)
print_pentagonal(3)
Code Solution
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
*
* * *
* * * * *
* * * *
* * *
Time Complexity
The time complexity of this program is O(n^2), where n is the side length. The program uses nested loops to iterate over the rows and columns of the pentagonal pattern. The outer loop runs for side*2-1 iterations, and the inner loop runs for a variable number of iterations depending on the current row. Therefore, the overall time complexity is quadratic in terms of the side length.
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