Pattern Printing in Kotlin.

What is pattern ?

Pattern printing is a common exercise in programming and is useful for building logic and understanding loops. Pattern printing involves printing out various shapes or figures made up of symbols such as asterisks or spaces. There are several different types of pattern printing, including right triangles, pyramids, diamonds, and many more.

To print patterns, you can use loops and conditional statements to generate the desired output. The outer loop is used to control the number of rows and the inner loop is used to control the number of symbols to print in each row. The conditional statements are used to determine when to print the symbol or when to print a space.

In programming, pattern printing exercises are commonly used to practice problem-solving skills and to develop a better understanding of loops, conditional statements, and other fundamental programming concepts. They are also commonly used in job interviews as a way to test candidates’ programming skills and ability to solve problems.

Different type of pattern printing in kotlin.

  1. Left triangle
  2. Inverted left triangle
  3. Pyramid
  4. Diamond
  5. Right triangle
  6. Inverted right triangle

Left Triangle

Kotlin
fun printRightTrianglePattern(n: Int) {
    for (i in 1..n) {
        for (j in 1..i) {
            print("* ")
        }
        println()
    }
}
fun main(){
    printRightTrianglePattern(5)
}

Inverted Left Triangle

Kotlin
fun Pattern(n: Int) {
    for (i in n downTo 1) {
        for (j in 1..i) {
            print("* ")
        }
        println()
    }
}
fun main(){
    Pattern(5)
}

Pyramid

Kotlin
fun Pattern(n: Int) {
    var k = 0
    for (i in 1..n) {
        for (space in 1..n - i) {
            print("  ")
        }
        while (k != 2 * i - 1) {
            print("* ")
            k++
        }
        k = 0
        println()
    }
}
fun main(){
    Pattern(5)
}

Diamond

Kotlin
fun Pattern(n: Int) {
    var k = 0
    for (i in 1..n) {
        for (space in 1..n - i) {
            print("  ")
        }
        while (k != 2 * i - 1) {
            print("* ")
            k++
        }
        k = 0
        println()
    }
    for (i in n - 1 downTo 1) {
        for (space in 1..n - i) {
            print("  ")
        }
        k = 0
        while (k != 2 * i - 1) {
            print("* ")
            k++
        }
        println()
    }
}
fun main(){
    Pattern(5)
}

Right Triangle

Kotlin
fun Pattern(n: Int) {
    for (i in 1..n) {
        for (j in n downTo 1) {
            if (j > i) {
                print("  ")
            } else {
                print("* ")
            }
        }
        println()
    }
}
fun main(){
    Pattern(5)
}

Inverted Right Triangle

Kotlin
fun Pattern(n: Int) {
    for (i in 1..n) {
        for (j in 1..n) {
            if (j < i) {
                print("  ")
            } else {
                print("* ")
            }
        }
        println()
    }
}
fun main(){
    Pattern(5)
}

Leave a Comment