Print simple 5 point star pattern
In this article, we will discuss how to print a simple 5-point star pattern using programming. We will provide a detailed explanation of the problem statement, present a suitable example, discuss the algorithm and pseudocode, and explain the resultant output. Additionally, we will analyze the time complexity of the code.
Problem Statement
The problem is to print a simple 5-point star pattern of a given height using ASCII characters. The star pattern should consist of spaces and symbols to form a star shape. The height represents the number of rows in the star pattern.
Example:
For a height of 9, the star pattern would look like this:
∘ ∘ ∘ ∘ ∘ ∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘ ∘∘ ∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘ ∘∘
Algorithm
To print the star pattern, we can use the following algorithm:
- Take the input for the height of the star pattern.
- Initialize variables for controlling the loops and points of the star pattern.
- Use nested loops to iterate through each row and column of the pattern.
- Within the loops, check the conditions to determine whether to print a symbol or a space character.
- Print the symbol or space character based on the conditions.
- Update the points for the next row.
- Repeat the steps 4-6 until all rows and columns have been processed.
- End the program.
Pseudocode
function display_star_pattern(height):
point_a1 = height + height / 2
point_a2 = point_a1
point_b1 = height + height + (height / 2) + 2
point_b2 = height / 2 - 2
for i = 0 to height - 1:
if i >= (height + 1) / 3:
point_b1 = point_b1 - 3
point_b2 = point_b2 + 3
for j = 0 to height * 3 - 1:
if j == point_a1 or j == point_a2:
print "∘"
else if (i >= height / 3 and (j == point_b1 or j == point_b2)) or (i == height / 3 and j + 1 != point_a1 and j - 1 != point_a1 and j + 1 != point_a2 and j - 1 != point_a2 and j % 2 == 0 and j > point_b2 and j < point_b1):
print "∘"
else:
print " "
point_a1 = point_a1 - 1
point_a2 = point_a2 + 1
print newline
Code Solution
Here given code implementation process.
//C Program
//Print simple 5 point star pattern
#include <stdio.h>
//Print star pattern of given height
void display_star_pattern(int height)
{
printf("\n Height : %d \n", height);
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
int point_a1 = height + height / 2;
int point_a2 = point_a1;
int point_b1 = height + height + (height / 2) + 2;
int point_b2 = height / 2 - 2;
//Loop, which is execute row operations
for (i = 0; i < height; ++i)
{
if (i >= (height + 1) / 3)
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
//Loop, which is perform column operations
for (j = 0; j < height * 3; j++)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
printf("∘");
}
//This condition are display the intermediate layers
else if (i >= ((height) / 3) && ((j == point_b1 || j == point_b2)) || (i == ((height) / 3) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
printf("∘");
}
else
{
printf(" ");
}
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
printf("\n");
}
}
int main()
{
//Simple test
display_star_pattern(9);
display_star_pattern(7);
display_star_pattern(13);
display_star_pattern(17);
display_star_pattern(25);
return 0;
}
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
// Java program
// Print simple 5 point star pattern
class MyPattern
{
//Print star pattern of given height
public void display_star_pattern(int height)
{
System.out.print("\n Height : " + height + " \n\n");
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
int point_a1 = height + height / 2;
int point_a2 = point_a1;
int point_b1 = height + height + (height / 2) + 2;
int point_b2 = height / 2 - 2;
//Loop, which is execute row operations
for (i = 0; i < height; ++i)
{
if (i >= (height + 1) / 3)
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
//Loop, which is perform column operations
for (j = 0; j < height * 3; j++)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
System.out.print("∘");
}
//This condition are display the intermediate layers
else if (i >= ((height) / 3) && ((j == point_b1 || j == point_b2)) || (i == ((height) / 3) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
System.out.print("∘");
}
else
{
System.out.print(" ");
}
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
System.out.print("\n");
}
}
public static void main(String[] args)
{
MyPattern obj = new MyPattern();
obj.display_star_pattern(9);
obj.display_star_pattern(7);
obj.display_star_pattern(13);
obj.display_star_pattern(17);
obj.display_star_pattern(25);
}
}
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
//Include header file
#include <iostream>
using namespace std;
// C++ program
// Print simple 5 point star pattern
class MyPattern
{
public:
//Print star pattern of given height
void display_star_pattern(int height)
{
cout << "\n Height : " << height << " \n\n";
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
int point_a1 = height + height / 2;
int point_a2 = point_a1;
int point_b1 = height + height + (height / 2) + 2;
int point_b2 = height / 2 - 2;
//Loop, which is execute row operations
for (i = 0; i < height; ++i)
{
if (i >= (height + 1) / 3)
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
//Loop, which is perform column operations
for (j = 0; j < height * 3; j++)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
cout << "∘";
}
//This condition are display the intermediate layers
else if (i >= ((height) / 3) && ((j == point_b1 || j == point_b2)) || (i == ((height) / 3) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
cout << "∘";
}
else
{
cout << " ";
}
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
cout << "\n";
}
}
};
int main()
{
MyPattern obj = MyPattern();
obj.display_star_pattern(9);
obj.display_star_pattern(7);
obj.display_star_pattern(13);
obj.display_star_pattern(17);
obj.display_star_pattern(25);
return 0;
}
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
//Include namespace system
using System;
// C# program
// Print simple 5 point star pattern
class MyPattern
{
//Print star pattern of given height
public void display_star_pattern(int height)
{
Console.Write("\n Height : " + height + " \n\n");
//Loop controlling variable [i,j]
int i = 0, j = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
int point_a1 = height + height / 2;
int point_a2 = point_a1;
int point_b1 = height + height + (height / 2) + 2;
int point_b2 = height / 2 - 2;
//Loop, which is execute row operations
for (i = 0; i < height; ++i)
{
if (i >= (height + 1) / 3)
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
//Loop, which is perform column operations
for (j = 0; j < height * 3; j++)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
Console.Write("∘");
}
//This condition are display the intermediate layers
else if (i >= ((height) / 3) && ((j == point_b1 || j == point_b2)) || (i == ((height) / 3) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
Console.Write("∘");
}
else
{
Console.Write(" ");
}
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
Console.Write("\n");
}
}
public static void Main(String[] args)
{
MyPattern obj = new MyPattern();
obj.display_star_pattern(9);
obj.display_star_pattern(7);
obj.display_star_pattern(13);
obj.display_star_pattern(17);
obj.display_star_pattern(25);
}
}
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
<?php
// Php program
// Print simple 5 point star pattern
class MyPattern
{
//Print star pattern of given height
public function display_star_pattern($height)
{
echo "\n Height : ". $height ." \n\n";
//Loop controlling variable [i,j]
$i = 0;
$j = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
$point_a1 = $height + intval($height / 2);
$point_a2 = $point_a1;
$point_b1 = $height + $height + (intval($height / 2)) + 2;
$point_b2 = intval($height / 2) - 2;
//Loop, which is execute row operations
for ($i = 0; $i < $height; ++$i)
{
if ($i >= intval(($height + 1) / 3))
{
$point_b1 = $point_b1 - 3;
$point_b2 = $point_b2 + 3;
}
//Loop, which is perform column operations
for ($j = 0; $j < $height * 3; $j++)
{
//Simple case
if ($j == $point_a1 || $j == $point_a2)
{
echo "∘";
}
//This condition are display the intermediate layers
else if ($i >= (intval(($height) / 3)) && (($j == $point_b1 || $j == $point_b2)) || ($i == (intval(($height) / 3)) && $j + 1 != $point_a1 && $j - 1 != $point_a1 && $j + 1 != $point_a2 && $j - 1 != $point_a2 && $j % 2 == 0 && ($j > $point_b2 && $j < $point_b1)))
{
echo "∘";
}
else
{
echo " ";
}
}
$point_a1 = $point_a1 - 1;
$point_a2 = $point_a2 + 1;
echo "\n";
}
}
}
function main()
{
$obj = new MyPattern();
$obj->display_star_pattern(9);
$obj->display_star_pattern(7);
$obj->display_star_pattern(13);
$obj->display_star_pattern(17);
$obj->display_star_pattern(25);
}
main();
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
// Node Js program
// Print simple 5 point star pattern
class MyPattern
{
//Print star pattern of given height
display_star_pattern(height)
{
process.stdout.write("\n Height : " + height + " \n\n");
//Loop controlling variable [i,j]
var i = 0;
var j = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
var point_a1 = height + parseInt(height / 2);
var point_a2 = point_a1;
var point_b1 = height + height + (parseInt(height / 2)) + 2;
var point_b2 = parseInt(height / 2) - 2;
//Loop, which is execute row operations
for (i = 0; i < height; ++i)
{
if (i >= parseInt((height + 1) / 3))
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
//Loop, which is perform column operations
for (j = 0; j < height * 3; j++)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
process.stdout.write("∘");
}
//This condition are display the intermediate layers
else if (i >= (parseInt((height) / 3)) && ((j == point_b1 || j == point_b2)) || (i == (parseInt((height) / 3)) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
process.stdout.write("∘");
}
else
{
process.stdout.write(" ");
}
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
process.stdout.write("\n");
}
}
}
function main()
{
var obj = new MyPattern();
obj.display_star_pattern(9);
obj.display_star_pattern(7);
obj.display_star_pattern(13);
obj.display_star_pattern(17);
obj.display_star_pattern(25);
}
main();
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
# Python 3 program
# Print simple 5 point star pattern
class MyPattern :
# Print star pattern of given height
def display_star_pattern(self, height) :
print("\n Height : ", height ," \n\n", end = "")
# Loop controlling variable [i,j]
i = 0
j = 0
# Initial point initialization
# points [a1,a2] are used to display pyramid
# points [a1,a2] are used to display intermediate symbol
point_a1 = height + int(height / 2)
point_a2 = point_a1
point_b1 = height + height + (int(height / 2)) + 2
point_b2 = int(height / 2) - 2
# Loop, which is execute row operations
while (i < height) :
if (i >= int((height + 1) / 3)) :
point_b1 = point_b1 - 3
point_b2 = point_b2 + 3
j = 0
# Loop, which is perform column operations
while (j < height * 3) :
# Simple case
if (j == point_a1 or j == point_a2) :
print("∘", end = "")
# This condition are display the intermediate layers
elif(i >= (int((height) / 3)) and((j == point_b1 or j == point_b2)) or(i == (int((height) / 3)) and j + 1 != point_a1 and j - 1 != point_a1 and j + 1 != point_a2 and j - 1 != point_a2 and j % 2 == 0 and(j > point_b2 and j < point_b1))) :
print("∘", end = "")
else :
print(" ", end = "")
j += 1
point_a1 = point_a1 - 1
point_a2 = point_a2 + 1
print("\n", end = "")
i += 1
def main() :
obj = MyPattern()
obj.display_star_pattern(9)
obj.display_star_pattern(7)
obj.display_star_pattern(13)
obj.display_star_pattern(17)
obj.display_star_pattern(25)
if __name__ == "__main__": main()
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
# Ruby program
# Print simple 5 point star pattern
class MyPattern
# Print star pattern of given height
def display_star_pattern(height)
print("\n Height : ", height ," \n\n")
# Loop controlling variable [i,j]
i = 0
j = 0
# Initial point initialization
# points [a1,a2] are used to display pyramid
# points [a1,a2] are used to display intermediate symbol
point_a1 = height + height / 2
point_a2 = point_a1
point_b1 = height + height + (height / 2) + 2
point_b2 = height / 2 - 2
# Loop, which is execute row operations
while (i < height)
if (i >= (height + 1) / 3)
point_b1 = point_b1 - 3
point_b2 = point_b2 + 3
end
j = 0
# Loop, which is perform column operations
while (j < height * 3)
# Simple case
if (j == point_a1 || j == point_a2)
print("∘")
end
# This condition are display the intermediate layers
elsif(i >= ((height) / 3) && ((j == point_b1 || j == point_b2)) || (i == ((height) / 3) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
print("∘")
else
print(" ")
end
j += 1
end
point_a1 = point_a1 - 1
point_a2 = point_a2 + 1
print("\n")
i += 1
end
end
end
def main()
obj = MyPattern.new()
obj.display_star_pattern(9)
obj.display_star_pattern(7)
obj.display_star_pattern(13)
obj.display_star_pattern(17)
obj.display_star_pattern(25)
end
main()
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
// Scala program
// Print simple 5 point star pattern
class MyPattern
{
//Print star pattern of given height
def display_star_pattern(height: Int): Unit = {
print("\n Height : " + height + " \n\n");
//Loop controlling variable [i,j]
var i: Int = 0;
var j: Int = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
var point_a1: Int = height + (height / 2).toInt;
var point_a2: Int = point_a1;
var point_b1: Int = height + height + ((height / 2).toInt) + 2;
var point_b2: Int = (height / 2).toInt - 2;
//Loop, which is execute row operations
while (i < height)
{
if (i >= ((height + 1) / 3).toInt)
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
j = 0;
//Loop, which is perform column operations
while (j < height * 3)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
print("∘");
}
//This condition are display the intermediate layers
else if (i >= (((height) / 3).toInt) && ((j == point_b1 || j == point_b2)) || (i == (((height) / 3).toInt) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
print("∘");
}
else
{
print(" ");
}
j += 1;
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
print("\n");
i += 1;
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyPattern = new MyPattern();
obj.display_star_pattern(9);
obj.display_star_pattern(7);
obj.display_star_pattern(13);
obj.display_star_pattern(17);
obj.display_star_pattern(25);
}
}
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
// Swift program
// Print simple 5 point star pattern
class MyPattern
{
//Print star pattern of given height
func display_star_pattern(_ height: Int)
{
print("\n Height : ", height ," \n");
//Loop controlling variable [i,j]
var i: Int = 0;
var j: Int = 0;
//Initial point initialization
//points [a1,a2] are used to display pyramid
//points [a1,a2] are used to display intermediate symbol
var point_a1: Int = height + height / 2;
var point_a2: Int = point_a1;
var point_b1: Int = height + height + (height / 2) + 2;
var point_b2: Int = height / 2 - 2;
//Loop, which is execute row operations
while (i < height)
{
if (i >= (height + 1) / 3)
{
point_b1 = point_b1 - 3;
point_b2 = point_b2 + 3;
}
j = 0;
//Loop, which is perform column operations
while (j < height * 3)
{
//Simple case
if (j == point_a1 || j == point_a2)
{
print("∘", terminator: "");
}
//This condition are display the intermediate layers
else if (i >= ((height) / 3) && ((j == point_b1 || j == point_b2)) || (i == ((height) / 3) && j + 1 != point_a1 && j - 1 != point_a1 && j + 1 != point_a2 && j - 1 != point_a2 && j % 2 == 0 && (j > point_b2 && j < point_b1)))
{
print("∘", terminator: "");
}
else
{
print(" ", terminator: "");
}
j += 1;
}
point_a1 = point_a1 - 1;
point_a2 = point_a2 + 1;
print("\n", terminator: "");
i += 1;
}
}
}
func main()
{
let obj: MyPattern = MyPattern();
obj.display_star_pattern(9);
obj.display_star_pattern(7);
obj.display_star_pattern(13);
obj.display_star_pattern(17);
obj.display_star_pattern(25);
}
main();
Output
Height : 9
∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘∘ ∘∘
Height : 7
∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 13
∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Height : 17
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
Height : 25
∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘ ∘
∘∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘ ∘∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘ ∘ ∘
∘ ∘
Resultant Output Explanation
The display_star_pattern
function is called with different heights as arguments to print multiple star patterns. Each pattern is printed using the ∘
symbol and spaces to form a 5-point star shape.
For example, when the function is called with a height of 9, the output shows the star pattern with a height of 9 rows. The spaces and symbols are arranged in such a way that they form a symmetrical star shape.
Time Complexity
The time complexity of the code can be analyzed based on the number of iterations in the nested loops.
Since the loops iterate over each row and column of the star pattern, the number of iterations is proportional to the height of the pattern. Let's denote the height as n
.
Therefore, the time complexity of the code is O(n^2)
.
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