How to print Hollow Pyramid Pattern in TypeScript - Step by Step Examples
How to print Hollow Pyramid Pattern in TypeScript ?
Answer
To print a hollow pyramid pattern in TypeScript, you can follow similar logic as JavaScript using nested loops.
✐ Examples
1 Hollow Pyramid Pattern (Height: 5) in TypeScript
In this example,
- Set the number of rows for the pyramid.
- Use nested loops: one for rows, one for spaces, and one for stars.
- In the inner loops, print spaces for the spaces between stars and print stars for the pyramid edges.
- Adjust conditions to print stars only at the pyramid edges and spaces elsewhere to create the hollow effect.
- Print each row to create the hollow pyramid pattern.
TypeScript Program
function printHollowPyramid(rows: number): void {
for (let i = 1; i <= rows; i++) {
let pattern = '';
for (let j = i; j < rows; j++) {
pattern += ' ';
}
for (let k = 1; k < (2 * i); k++) {
if (k === 1 || k === (2 * i - 1) || i === rows) {
pattern += '* ';
} else {
pattern += ' ';
}
}
console.log(pattern);
}
}
printHollowPyramid(5);
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in TypeScript language with well detailed examples.
More TypeScript Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in TypeScript ?
- How to print Right Half Pyramid Pattern in TypeScript ?
- How to print Pyramid Pattern in TypeScript ?
- How to print Rhombus Pattern in TypeScript ?
- How to print Diamond Pattern in TypeScript ?
- How to print Hour Glass Pattern in TypeScript ?
- How to print Hollow Square Pattern in TypeScript ?
- How to print Hollow Pyramid Pattern in TypeScript ?
- How to print Hollow Inverted Pyramid Pattern in TypeScript ?
- How to print Hollow Diamond Pattern in TypeScript ?
- How to print Floyd's Trianlge Pattern in TypeScript ?
- How to print Pascal's Triangle Pattern in TypeScript ?