0
public static int calculate(int n){
    if (n <= 1) {
        return 1;
    } else if (n >= 2 && n%2 == 0){
        return (n * calculate(n - 2));
    } else if (n >= 3 && n%2 == 1){
        return n-1 * calculate(n-2);
    } else {
        return n;
    }

I need it to make a numerical sequence for odd numbers:

(n-1) * (n-2) * (n-2)..... * 2

For example:

n=9 == 8 * 6 * 4 * 2 or n=11 == 10 * 8 * 6 .....

And this line return n-1 * calculate(n-2) is not working.

wdwizzard
  • 1
  • 1
  • Presumably you mean `(n-1) * calculate(n-2)`. But the compiler will require a return statement for the case where all the if conditions are false. – khelwood Aug 05 '22 at 10:33
  • ohh sory, I didn't paste all the code. I have a return statement for the case where all if are false. I'm gonna change it in post – wdwizzard Aug 05 '22 at 10:41
  • @wdwizzard I voted to reopen, but for the future you should post a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve) to avoid such occurrences. – Federico klez Culloca Aug 05 '22 at 10:43
  • Okey, thx a lot and sorry for my mistake :) – wdwizzard Aug 05 '22 at 10:45
  • @OP did that change (the parenthesis around the `(n-1)`) fix your issue? If so, this question should be closed (by others) as an issue caused by a typo / not reproducible, since it's something unlikely to help future readers. – Rogue Aug 05 '22 at 12:01

1 Answers1

0

You missed on a bracket because of which, the order of operations went wrong and you were getting wrong outputs...

Instead of return n-1 * calculate(n-2), you must write

return (n-1) * calculate(n-2)

Now you will get the correct output...

Utkarsh Sahu
  • 409
  • 3
  • 16