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.
In this example,
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()
}
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Pyramid Pattern in Kotlin language with well detailed examples.