Home > Software design >  print "X" star patterns in Kotlin
print "X" star patterns in Kotlin

Time:02-24

can someone help me make star patterns like this using for loop in kotlin? i already try this but i think my code is too long. can someone help me?

x Star Pattern

fun fifthPyramid(){
    for(i in 1..13){
        if(i==1||i==13){
            print("*")
        }else
            print(" ")
    }
    println("")
    for(i in 1..13){
        if(i==2||i==12){
            print("*")
        }else
            print(" ")
    }
    println("")
    for(i in 1..13){
        if(i==3||i==11){
            print("*")
        }else
            print(" ")
    }
    println("")
    for(i in 1..13){
        if(i==4||i==10){
            print("*")
        }else
            print(" ")
    }
    println("")
    for(i in 1..13){
        if(i==5||i==9){
            print("*")
        }else
            print(" ")
    }
}

CodePudding user response:

The star pattern consists of exactly N * 2 - 1 rows and columns. So the outer and inner loop will run till towards count = N * 2 - 1

fun main() {

    var starCount = 5;
    val count = starCount * 2 - 1; 

    for(i in 1..count){
        for(j in 1..count){
            if(j==i || (j==count - i   1))
            {
                print("*");
            }
            else
            {
                print(" ");
            }
        }
        
        println("")
    }

}

CodePudding user response:

Identify the pattern with respect to row and column. You put a * when the row and column are the same, or the row and inverse of the column are the same.

fun printX(size: Int, char: Char) {
    repeat(size) { row ->
        repeat(size) { col ->
            print(if (row == col || row == (size - col - 1)) char else ' ')
        }
        println()
    }
}

fun main() {
    printX(7, '*')
}

CodePudding user response:

You can write a fun that takes an Int and a Char as arguments and then use a loop in order to build each line and print it.

Basically like this:

fun printX(heightWidth: Int, symbol: Char) {
    // iterate the heigth in order to build up each line (top->down)
    for (i in 0 until heightWidth) {
        /*
          build up an array of Chars (representing a line)
          with the desired size (length of a line)
          filled up with whitespaces by default
         */
        var line = CharArray(heightWidth) { ' ' }
        // then replace whitespaces at the desired indexes by the symbol
        line[i] = symbol    // one from left to right
        line[line.size - i - 1] = symbol    // and one from right to left
        // and print the result
        println(line)
    }
}

You can throw an Exception in case of negative heightWidth values because they will cause trouble if you don't. And maybe forbid 0, 1 and 2, because although they would produce valid output, that output can hardly be regarded as X. Even the output for 3 and 4 is rather ugly ;-)

However, here's the output of printX(7, '7'):

7     7
 7   7 
  7 7  
   7   
  7 7  
 7   7 
7     7
  • Related