How to find sum of the first N even numbers in Kotlin

1 Answer

0 votes
fun main() {
    val N = 5

    // The sum of the first N even numbers is N * (N + 1)
    var sumEven: Int = N * (N + 1)

    println("The sum of the first $N even numbers is: $sumEven")
}

// 2 + 4 + 6 + 8 + 10 = 30


/*
run:

The sum of the first 5 even numbers is: 30

*/

 



answered Sep 6, 2025 by avibootz
...