Average of an Array

fun main() {
    println("Enter the size of Array")//1
    val size = readLine()?.toInt()!!//2
    val numbers = IntArray(size)//3
    for(i in 0 until size)//4
    {
        numbers[i]= readLine()?.toInt()!!//5

    }
    var sum=0.0//6
    for(i in numbers)//7
    {
        sum+=i;//8
    }

    println("Average of Array=${sum/size}")//9
}
  1. Instruct the user to Enter the size of the Array
  2. Convert the entered value into int and assign to size variable
  3. Create an array to Int of a given size
  4. Execute the for loop from 0 to less than the size
  5. Initialized the elements of array by user-entered values
  6. Create and initialize the value of the variable sum
  7. Execute for loop statement to add all elements
  8. Add All elements values into sum variable calculate the sum of array
  9. Calculate and Print the output of Average of Array Elements on console

Output

Enter the size of Array
5
10
45
67
89
90
Average of Array=60.2

Leave a Reply

Your email address will not be published. Required fields are marked *