+ 1
why doesn't this code work
a function to calculate factorial, but it throws an exception when n is above 12 or below 0 https://code.sololearn.com/cVU8xHE8m69F/?ref=app
2 Answers
+ 1
public int factorial(int n) throws IllegalArgumentException {
int factorial = 0;
if (n>12 || n<0){ throw new IllegalArgumentException(); }
else {
factorial = 1;
for (int i=n;i>=1;i--){
factorial *= i;
}
}
return factorial;
}
}
please try this function
+ 1
1) you should put a semi-colon at end of all statements (missing one at end of IllegalArgumentException())
2) you must declare int factorial outside of if block to be accessible to return statement ^^