Kotlin Program to find the sum of even and odd elements in an array


In this program, You will learn how to find the sum of even and odd elements in an array in Kotlin.


list are: 6 7 8 9 10

Even list sum is: 24

Odd list sum is: 16

Example: How to find the sum of even and odd elements in an array in Kotlin.

import java.util.Scanner

fun main(args: Array<String>) {

    var se = 0
    var sod = 0

    val arr = IntArray(5)
    val sc = Scanner(System.`in`)

    print("Enter 5 numbers:")

    for (i in 0 until arr.size) {
        arr[i] = sc.nextInt()
    }

    for (n in arr) {
        if (n % 2 == 0) {
            se += n
        } else {
            sod += n
        }
    }

    println("Sum of even list:$se")
    println("Sum of odd list:$sod")
}

Output:

Enter 5 numbers:2 3 4 5 6
Sum of even list:12
Sum of odd list:8