answersLogoWhite

0

I can't imagine a useful reason to have a recursive function to find this, but here you go:

int sumEvens(int start, int end) {

// end condition

if (start > end) {

return 0;

}

// correction if we start on an odd number

if (start % 2 == 1) {

return sumEvens(start + 1, end);

}

// actual work

return start + sumEvens(start + 2, end);

}

Invoke with sumEvens(2, 50) to get the sum of all even numbers in the range [2,50]

User Avatar

Wiki User

15y ago

What else can I help you with?