To print a Diamond Pattern in Kotlin, we use nested loops. The outer loop controls the rows and the inner loops control the spaces and stars to form the pattern.
In this example,
n
to represent the desired number of rows in the diamond pattern.for
loops, where the outer loop runs from 1 to n
and the inner loops control the spaces and stars in each row.fun main() {
val n = 5
for (i in 1..n) {
for (j in 1..n - i) print(' ')
for (j in 1..2 * i - 1) print('*')
println()
}
for (i in n - 1 downTo 1) {
for (j in 1..n - i) print(' ')
for (j in 1..2 * i - 1) print('*')
println()
}
}
* *** ***** ******* ********* ******* ***** *** *
In this tutorial, we learned How to print Diamond Pattern in Kotlin language with well detailed examples.