int f(int n, int k) {
if (n == 0) return 0;
else if (n % 2) return f(n/2, 2*k) + k; // This condition is true when n%2 is 1
else return f(n/2, 2*k) - k; // otherwise
}

bthebestSelf you are doing n%2 = 0 as true, then you get -15 as the answer. If you doing n%2 = 0 as false then you get 9 as an answer :)

Else if (n%2) when n is multiple of 2 it will be like else if (0) therefore it will return false and will execute next else and return f(n/2,2*k) - K....