Throw an exception for values over 16 since it will cause an integer overflow.
	The Exception message is used in main the same is for -ve values.
This commit is contained in:
HeshamTB 2020-10-15 06:44:51 +03:00
parent e6b7cdc211
commit 27234cf989
Signed by: Hesham
GPG Key ID: 74876157D199B09E

View File

@ -25,6 +25,7 @@ public class Factorials{
private static int factorial(int value) throws IllegalArgumentException {
if (value < 0) throw new IllegalArgumentException("Can not calculate a negative factorial");
if (value > 16) throw new IllegalArgumentException("Can not calculate factorials for value over 16");
for (int i = value - 1; i > 0; i--){
value *= i;
}