Created
October 19, 2023 11:50
-
-
Save sasssass/259082d81329a0463f2c23b9b88e8dc8 to your computer and use it in GitHub Desktop.
functional.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val numbers = intArrayOf(1, 2, 3, 4, 5, 6, 7, 8, 9, 10) | |
val sum = sumOfEvenNumbers(numbers, 0, 0) | |
println("Sum of even numbers: $sum") | |
} | |
fun sumOfEvenNumbers(numbers: IntArray, index: Int, currentSum: Int): Int { | |
return when { | |
index == numbers.size -> currentSum | |
numbers[index] % 2 == 0 -> sumOfEvenNumbers(numbers, index + 1, currentSum + numbers[index]) | |
else -> sumOfEvenNumbers(numbers, index + 1, currentSum) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment