bom's happy life

for문을 활용한 삼각형 출력하기 본문

Deveolpment Study🗂️/Kotlin

for문을 활용한 삼각형 출력하기

bompeach 2023. 2. 15. 12:45
for문을 활용한 삼각형 출력하기

n: 줄의 수 입력
반복(line: 1 -> n만큼) {
  반복(space: 1 ->(n-line)만큼) {공백출력}
  반복(star: 1 -> (2*line-1)만큼) {별표출력}
  개행
}

 

 

//for문을 활용한 삼각형 출력하기

fun main() {
    print("Enter the lines: ")
    val n = readLine()!!.toInt() //콘솔로부터 입력받음

    for(line in 1..n) {
        for(space in 1..(n-line)) print(" ") //공백출력
        for(star in 1..(2*line-1)) print("*") //별표출력
        println()
    }
}

실행결과