Kotlin Program using an array with a loop


In this program, You will learn how to implement an array with a loop in Kotlin.


Array list is: 2 3 4 5 6

Example: How to implement an array with a loop in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var arr = IntArray(100)
    var n: Int
    val sc = Scanner(System.`in`)

    print("Enter size of an array:")
    n = sc.nextInt()

    print("Enter $n elements :")
    for (i in 0 until n) {
        arr[i] = sc.nextInt()
    }

    for (i in 0 until n) {
        println("item :${arr[i]} ")
    }
}

Output:

Enter size of an array:5
Enter 5 elements :10 20 30 40 50
item :10 
item :20 
item :30 
item :40 
item :50