A hollow pyramid pattern is a triangular design in which only the outline is visible, and the interior part of the pattern is empty. In programming, printing Characters/Numbers/Special Symbols in a particular pattern that forms a shape's outline but leaves its interior empty is commonly known as hollow patterns.
Why is it Important to Learn the Hollow Pyramid Pattern Program?
The hollow pattern program will help you better understand nested loop concepts and conditional statements while improving your problem-solving and logical thinking.
It allows programmers to experiment with various styles while working with graphics-based applications. Learning different patterns allows architects to experiment with different architectural layouts and configurations.
Pattern programs, including hollow pyramid pattern programs, are common questions in technical interviews. Practicing these can help you prepare for coding interviews
The concept of hollow patterns is frequently used for real-world applications such as
1. Image Processing:
Pattern programs can detect objects in images by comparing the pattern to the pixels. It can also be used to segment images based on a specific pattern.
2. Game Development:
The Pattern program can create graphics and visual elements for games, such as menus, buttons, options, dialogue boxes, etc
3. 3D Model:
Pattern programs can generate 3D mesh models with hollow interiors. It allows you to print objects with resonating sections.
4. Geographic Information System:
The Pattern program can be used to create maps and analyse spatial data.
How to Create a Hollow Pyramid Pattern in Java?
1. Hollow Pyramid Pattern Using Star in Java
i. Code:
public class HollowPyramidPattern {
public static void main(String[] args) {
int rows = 6;
System.out.println("Pyramid Pattern of Stars by Itvedant");
int i, j, k;
for (i = 1; i <= rows; i++) {
for (j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
if (i == 1 || i == rows) {
for (k = 1; k <= (i * 2) - 1; k++) {
System.out.print("*");
}
} else {
for (k = 1; k <= (i * 2) - 1; k++) {
if (k == 1 || k == i * 2 - 1) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
ii. Output:
iii. Explanation:
Let’s understand the Hollow Pattern Program.
j=1 | j=2 | j=3 | j=4 | j=5 | |||||||
i=1 | * | ||||||||||
i=2 | * | * | |||||||||
i=3 | * | * | |||||||||
i=4 | * | * | |||||||||
i=5 | * | * | |||||||||
i=6 | * | * | * | * | * | * | * | * | * | * | * |
As it is seen in above Fig. there are 6 rows and 11 Columns where we have to print the hollow pattern.
The code to print hollow patterns is divided into 3 parts i.e. Part 1: space before asterisk marks, part 2: asterisk mark itself and part 3: space and asterisk mark. To jump from one row to another row, the outer “for” loop with the iterator variable “i” is considered. So, all three parts of the code will be executed in each row before we jump to the next row.
Firstly, we will understand how to add spaces before a pattern starts. We are considering the nested for loop with the iterator variable “j” to iterate over the column part where space is supposed to be printed. Now let's analyse the relation between the iterator variables “i” and “j”.
For i= | j start | j end (i.e. total no.of spaces printed in that row) | Relation between the Current value of i and j end value |
1 | 1 | 5 | j end (5) = 6 – current value of i (1) |
2 | 1 | 4 | j end (4) = 6 – current value of i (2) |
3 | 1 | 3 | j end (3) = 6 – current value of i (3) |
4 | 1 | 2 | j end (2) = 6 – current value of i (4) |
5 | 1 | 1 | j end (1) = 6 – current value of i (5) |
6 | 1 | 0 | j end (0) = 6 – current value of i (6) |
The above table shows that the starting value of “j” is always 1, but the ending value of the j iterator variable or space is always different. However, there is a relationship between the current value of “i” and the ending value of “j”, as shown in the table above.
The ending value of “j” always equals the total rows - the current value of “i”. Considering the same, an inner for loop is designed, with the ending value of “j” as j = rows - i.
Now, let’s understand the second part of the pattern which is printing asterisk marks on the first and last line. For printing asterisk marks and space between the pattern iterator variable k is considered.
i=1 | k=1* | ||||||||||
i=2 | * | * | |||||||||
i=3 | * | * | |||||||||
i=4 | * | * | |||||||||
i=5 | * | * | |||||||||
i=6 | *k=1 | *k=2 | *k=3 | *k=4 | *k=5 | *k=6 | *k=7 | *k=8 | *k=9 | *k=10 | *k=11 |
As seen in the above figure when the value of i=1 at that time only 1 asterisk (*) is printed and when the value of i=6, the number of asterisks is 11 so, considering the same starting value of “k” will be 1 for i=1 and i=6 and the ending condition of k will k<=(i*2)-1 which matches the number of asterisk marks that are printed for i=1 and for i=6.
The above results are achieved using the following code :
if(i == 1 || i == rows)
{
for (k = 1 ; k <= (i * 2) - 1; k++ )
{
System.out.print("*");
}
}
Finally, the further pattern with asterisk marks and hollow space can be achieved using the below code:
for (k = 1; k <= (i * 2) - 1; k++ )
{
if(k == 1 || k == i * 2 - 1) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
k=1 | |||||||||||
i=1 | * | ||||||||||
k=1 | k=2 | k=3 | |||||||||
i=2 | * | * | |||||||||
k=1 | k=2 | k=3 | k=4 | k=5 | |||||||
i=3 | * | * | |||||||||
k=1 | k=2 | k=3 | k=4 | k=5 | k=6 | k=7 | |||||
i=4 | * | * | |||||||||
k=1 | k=2 | k=3 | k=4 | k=5 | k=6 | k=7 | k=8 | k=9 | |||
i=5 | * | * | |||||||||
i=6 | * | * | * | * | * | * | * | * | * | * | * |
In the above table it can be seen that for rows starting from i=2 to i=5, there is an asterisk (*) mark for k=1 and k <= (i * 2) – 1 so if the given condition is satisfied we will print asterisk marks and if the condition is not satisfied space is printed.
2. Hollow Pyramid Pattern Using Numbers in Java
i. Code:
import java.util.Scanner;
public class HollowPyramidPattern {
public static void main(String[] args) {
int rows = 6;
int number;
System.out.println("Pyramid Pattern of Numbers by Itvedant");
int i, j, k;
for (i = 1; i <= rows; i++) {
number = 1;
for (j = 1; j <= rows - i; j++) {
System.out.print(" ");
}
if (i == 1 || i == rows) {
for (k = 1; k <= (i * 2) - 1; k++) {
System.out.print(number);
number++;
}
} else {
for (k = 1; k <= (i * 2) - 1; k++) {
if (k == 1 || k == (i * 2) - 1) {
System.out.print(number);
number++;
} else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
ii. Output:
iii. Explanation:
The hollow pyramid pattern with the number has the same pattern as the asterisk hollow pyramid. The only difference is in every row a number replaces the asterisk.
So, a new variable “number” is considered which is initialized with value 1 for every new value of i(which represents row). Asterisk marks in the previous program are replaced with the “number” variable and the value of the “number” variable is incremented by 1 after the value is printed. If you’re planning to become a Java developer, it’s time to check out our Java Course with a 100% Job Guarantee