+ 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

27th Feb 2021, 9:45 AM
Sheldon 10110
Sheldon 10110 - avatar
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
27th Feb 2021, 9:51 AM
Ketul Patel
Ketul Patel - avatar
+ 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 ^^
27th Feb 2021, 12:33 PM
visph
visph - avatar