The reason it works for even numbers is because you are hard coding the corresponding stop condition at the beginning of your function.
Look at your recursive function. If the number is even, there will be recursive calls until n == 0; That is, until we get here:
if (n <= 0) return 1;
From there, we compute the final result bottom up.
return 3 * 1 -3; //that's 0 return 3 * 0 -3; //that's -3 return 3 * -3 -3; //that's -12 return 3 * 3 -3; //that's -39 //...and so on
In case the number is odd, we start from 2, because of this line:
else if (n == 1) return 2;
From there, we compute the final result bottom up.
return 3 * 2 -3; //that's 3 return 3 * 3 -3; //that's 6 return 3 * 6 -3; //that's 15 return 3 * 15 -3; //that's 42 //... and so on
Your iterative function starts like this.
int b = 1 ;
That is, you're imposing a condition that should only be there in case the number is even. Instead, you should test if the number is even or odd.
if (a % 2 == 0) b = 1; else b = 2; for (int i = b; i <= a; i = i+2) { //... }