Kotlin: Find the Sum of Elements in an Array

1. Introduction

Summation is one of the most fundamental operations applied to collections of numbers. Arrays in Kotlin, with their intuitive operations, make such tasks straightforward. In this tutorial, we'll learn how to use Kotlin to compute the sum of all elements contained within an array.

2. Program Overview

Our Kotlin program will:

1. Initiate an array of numbers.

2. Traverse this array to accumulate the sum of its elements.

3. Display the resulting sum to the user.

3. Code Program

fun main() {
    // Kick off an array of numbers
    val numbers = arrayOf(12, 24, 36, 48, 60, 72)

    // Set the stage to compute the total sum of the array's elements
    var sum = 0
    for (num in numbers) {
        sum += num

    // Show the computed sum to the user
    println("The sum of all the elements in the array is: $sum")


The sum of all the elements in the array is: 252

4. Step By Step Explanation

1. Array Initialization: We begin with creating an array called numbers, that holds some sample integer values.

2. Sum Calculation:

- We initialize the variable sum to 0.

- A for-loop iterate through each number in the array. With every iteration, the current number gets added to our sum.

3. Output Presentation: Once the sum calculated, the program displays the sum to the user.