Kotlin recursive function: Sum of even numbers in a range
Write a Kotlin recursive function to calculate the sum of all even numbers in a range.
Pre-Knowledge (Before You Start!)
Before solving this exercise, you should be familiar with the following concepts:
- Recursion: A method where a function calls itself to break down the problem into smaller subproblems. In this case, recursion is used to calculate the sum of even numbers in a range.
- Modulo Operator (%): The modulo operator is used to find the remainder when one number is divided by another. In this case, it's used to check if a number is even (start % 2 == 0).
- Base Case in Recursion: The base case is the condition under which the recursive function stops. For this problem, if the start number is greater than the end, the recursion stops and returns 0.
- Recursive Case: In this case, you either add the current even number to the sum and move to the next even number, or skip the odd number and move to the next one.
- Range of Numbers: The given range is defined by the start and end parameters. We need to sum all even numbers between these two values.
Hints (Try Before Looking at the Solution!)
Here are some hints to help you solve the problem:
- Hint 1: Use recursion to sum the even numbers within the range. The base case should stop the recursion when the start number exceeds the end number.
- Hint 2: If the current number (start) is even, add it to the sum and move to the next even number by incrementing by 2.
- Hint 3: If the current number (start) is odd, skip it by incrementing by 1 and check the next number.
- Hint 4: Ensure that the recursion stops when the start number is greater than the end number (base case).
- Hint 5: Use the recursive call to compute the sum and return it back to the previous call until the range is fully processed.
Sample Solution:
Kotlin Code:
fun sumEvenNumbers(start: Int, end: Int): Int {
if (start > end) {
return 0
}
return if (start % 2 == 0) {
start + sumEvenNumbers(start + 2, end)
} else {
sumEvenNumbers(start + 1, end)
}
}
fun main() {
val start = 10
val end = 30
val sum = sumEvenNumbers(start, end)
println("Sum of even numbers from $start to $end: $sum")
}
Sample Output:
Sum of even numbers from 10 to 30: 220
Explanation:
In the above exercise -
- The function "sumEvenNumbers()" takes two parameters: start and end, representing the range of numbers to consider.
- In the base case, if start is greater than end, it means we have reached the end of the range, so we return 0.
- If start is an even number (start % 2 == 0), we add it to the sum of the remaining even numbers in the range. We recursively call the "sumEvenNumbers()" function with start + 2 to consider the next even number.
- If start is an odd number, we skip it and move to the next number by recursively calling the "sumEvenNumbers()" function with start + 1.
- The recursive calls continue until the base case is reached and the final sum is computed.
Kotlin Editor:
Previous: Binary tree as a binary search tree.
Next: Calculate factorial.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics