Kotlin Program to find the sum of array elements
In this program, You will learn how to find the sum of array elements in Kotlin.
List is: 2 3 4 5 6
2 + 3 + 4 + 5 + 6 => 20
Example: How to find the sum of array elements in Kotlin.
import java.util.Scanner
fun main(args: Array<String>) {
var i = 0
var s = 0
val arr = IntArray(5)
val sc = Scanner(System.`in`)
print("Enter 5 numbers:")
while (i < 5) {
arr[i] = sc.nextInt()
i++
}
i = 0
while (i < 5) {
s += arr[i]
i++
}
println("Sum of all elements:$s")
}
Output:
Enter 5 numbers:10 20 30 40 50
Sum of all elements:150