To create a list of numbers from 1 to N in Kotlin, you can use the List constructor and a lambda function to initialize each element with its index plus one.
In this example,
fun main() {
val N = 5
val list = List(N) { it + 1 }
println("List of numbers from 1 to $N: $list")
}
List of numbers from 1 to 5: [1, 2, 3, 4, 5]
In this tutorial, we learned How to create a List of Numbers from 1 to N in Kotlin language with well detailed examples.