To print a Hollow Square Pattern in Kotlin, we use nested loops. The outer loop controls the rows, and the inner loops control the columns and the conditions for printing the hollow square pattern.
In this example,
printHollowSquare
that takes the size of the square n
as a parameter.for
loops to iterate over rows and columns.fun printHollowSquare(n: Int) {
for (i in 0 until n) {
for (j in 0 until n) {
if (i == 0 || i == n - 1 || j == 0 || j == n - 1)
print("*")
else
print(" ")
}
println()
}
}
// Example usage
fun main() {
printHollowSquare(5)
}
***** * * * * * * *****
In this tutorial, we learned How to print Hollow Square Pattern in Kotlin language with well detailed examples.