w3resource

Kotlin recursive function: Product of odd numbers in a range


Write a Kotlin recursive function to calculate the product of all odd numbers in a range.


Pre-Knowledge (Before You Start!)

Before solving this exercise, you should understand these concepts:

  • Recursion: A recursive function calls itself with a smaller problem until a base case is met.
  • Base Case: The recursion stops when the start value exceeds the end value.
  • Odd Numbers: Odd numbers are numbers that are not divisible by 2 (e.g., 1, 3, 5, etc.).
  • Multiplication in Recursion: The function multiplies the current odd number with the recursive call result.
  • Skipping Even Numbers: If the current number is even, the function moves to the next odd number.

Hints (Try Before Looking at the Solution!)

Here are some hints to help you solve the problem:

  • Hint 1: Use recursion to multiply odd numbers in the given range.
  • Hint 2: Stop recursion when the start value exceeds the end value.
  • Hint 3: Check if the current number is odd before multiplying.
  • Hint 4: If the current number is even, skip to the next odd number.
  • Hint 5: Use 1 as the base case since multiplying by 1 doesn’t change the result.

Sample Solution:

Kotlin Code:

fun calculateProductOfOdds(start: Int, end: Int): Long {
    if (start > end) {
        return 1
    }

    return if (start % 2 != 0) {
        start.toLong() * calculateProductOfOdds(start + 2, end)
    } else {
        calculateProductOfOdds(start + 1, end)
    }
}

fun main() {
    val start = 1
    val end = 15
    val product = calculateProductOfOdds(start, end)
    println("Product of odd numbers between $start and $end: $product")
}

Sample Output:

Product of odd numbers between 1 and 15: 2027025

Explanation:

In the above exercise -

  • The "calculateProductOfOdds()" function takes the start and end values of the range as parameters. This function recursively calculates the product of odd numbers by checking the current number (starting with start) and determining whether it is odd or even.
  • If the current number is odd (i.e., start % 2 != 0), it multiplies the current number with the recursive call to calculateProductOfOdds for the next odd number (start + 2) and continues the recursion.
  • In the case of even numbers, calculateProductOfOdds is called with start + 1 to skip to the next odd number.
  • The recursion continues until the start exceeds end value. At that point, it returns 1, as 1 multiplied with any number doesn't affect the product.
  • In the "main()" function, we define the start and end values of the range (1 and 15, respectively), and then call the "calculateProductOfOdds()" function with these values. It is then printed to the console with the calculated product.

Kotlin Editor:


Previous: Generate permutations of a string.
Next: Find the nth term of arithmetic sequence.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.