In this tutorial, you shall learn how to write a program to find the average of numbers given in an array in Kotlin.
Kotlin – Average of Numbers Program
To find the average of multiple numbers given in an array in Kotlin, find the sum of the given numbers and then divide the sum by the count of numbers given.
Program
In the following program, we take an array of numbers in numsArray
, then find the average of the numbers in the array.
Main.kt
</>
Copy
fun main() {
val numsArray = arrayOf(4, 0, 5, 2, 10, 3, 8)
var sum = 0
for (num in numsArray) {
sum += num
}
val avg = sum.toDouble() / numsArray.size
println("Average : $avg.")
}
Output #1
Average : 4.571428571428571.
Related Tutorials
Conclusion
In this Kotlin Tutorial, we learned how to find the average of given numbers.