How to print Hollow Pyramid Pattern in Kotlin - Step by Step Examples
How to print Hollow Pyramid Pattern in Kotlin ?
Answer
To print a hollow pyramid pattern in Kotlin, you can use nested loops. The outer loop controls the rows, and the inner loops manage the spaces and stars to create the hollow effect.
✐ Examples
1 Hollow Pyramid Pattern of height 5
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.
Kotlin Program
fun main() {
val rows = 5
for (i in 1..rows) {
// Print spaces
for (j in i until rows) {
print(" ")
}
// Print stars for the pyramid
for (k in 1 until (2 * i)) {
if (k == 1 || k == (2 * i - 1) || i == rows) {
print("* ")
} else {
print(" ")
}
}
println()
}
}
Output
* * * * * * * * * * * * * * * *
Summary
In this tutorial, we learned How to print Hollow Pyramid Pattern in Kotlin language with well detailed examples.
More Kotlin Pattern Printing Tutorials
- How to print Left Half Pyramid Pattern in Kotlin ?
- How to print Right Half Pyramid Pattern in Kotlin ?
- How to print Pyramid Pattern in Kotlin ?
- How to print Rhombus Pattern in Kotlin ?
- How to print Diamond Pattern in Kotlin ?
- How to print Hour Glass Pattern in Kotlin ?
- How to print Hollow Square Pattern in Kotlin ?
- How to print Hollow Pyramid Pattern in Kotlin ?
- How to print Hollow Inverted Pyramid Pattern in Kotlin ?
- How to print Hollow Diamond Pattern in Kotlin ?
- How to print Floyd's Trianlge Pattern in Kotlin ?
- How to print Pascal's Triangle Pattern in Kotlin ?