Write a Kotlin Program To Create Pyramid and Pattern

In Kotlin, we can create pyramid and pattern using loops and conditional statements.

Here’s an example of how we can create a pyramid and pattern:

Pyramid:

fun main() {
    val rows = 5
    var k = 0
    for (i in 1..rows) {
        k = 0
        for (j in 1..rows - i) {
            print("  ")
        }
        while (k != 2 * i - 1) {
            print("* ")
            ++k
        }
        println()
    }
}

Output:

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

In this example, we first define the number of rows for our pyramid (in this case, 5).

We then use nested loops to print out the pyramid. The outer loop iterates through each row of the pyramid, while the inner loop handles the spaces before the asterisks.

Pattern:

fun main() {
    val rows = 5
    for (i in 1..rows) {
        for (j in 1..i) {
            print("* ")
        }
        println()
    }
}

Output:

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

In this example, we use a single loop to print out the pattern.

The loop iterates through each row of the pattern, and another loop prints out the asterisks for each row.

These examples are just a starting point – there are many other ways to create pyramids and patterns using Kotlin.

With practice, you can create more complex designs and patterns using loops and conditional statements.