To print a Hollow Diamond Pattern in Kotlin, we use nested loops to manage spaces and stars.
In this example,
fun main() {
val numRows = 5
for (row in 1..numRows) {
for (space in 1..numRows - row) {
print(" ")
}
for (star in 1..(2 * row - 1)) {
if (star == 1 || star == 2 * row - 1) {
print("*")
} else {
print(" ")
}
}
println()
}
for (row in numRows - 1 downTo 1) {
for (space in 1..numRows - row) {
print(" ")
}
for (star in 1..(2 * row - 1)) {
if (star == 1 || star == 2 * row - 1) {
print("*")
} else {
print(" ")
}
}
println()
}
}
* * * * * * * * * * * * * * * *
In this tutorial, we learned How to print Hollow Diamond Pattern in Kotlin language with well detailed examples.