X268: Recursion Programming Exercise: Add odd values

For function addOdd(n) write the missing recursive call. This function should return the sum of all postive odd numbers less than or equal to n.

Examples:

addOdd(1) -> 1
addOdd(2) -> 1
addOdd(3) -> 4
addOdd(7) -> 16

Your Answer:

x
 
1
public int addOdd(int n) {
2
  if (n <= 0) {
3
    return 0;
4
  }
5
  if (n % 2 != 0) { // Odd value
6
    return <<Missing a Recursive call>>
7
  } else { // Even value
8
    return addOdd(n - 1);
9
  }
10
}
11

Feedback

Your feedback will appear here when you check your answer.