How to print organized patterns in Kotlin?

In Kotlin, nested loops can be used to print a pattern. Here is an example code for printing a rectangular pattern.

fun main() {
    val rows = 5
    val columns = 10

    for (i in 0 until rows) {
        for (j in 0 until columns) {
            print("* ")
        }
        println()
    }
}

In this example, we are defining the rows and columns variables to specify the number of rows and columns in the rectangular pattern. We then use nested for loops to iterate through each row and column, printing the * symbol. When the inner loop finishes printing a row, we use println() to move to the next line.

Output: The result displayed is…

* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 
* * * * * * * * * * 

You can adjust the values of rows and columns as needed to resize the pattern. Also, you can print other regular patterns by modifying the printing content of the inner loop.

bannerAds