-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathUtils.java
More file actions
27 lines (24 loc) · 830 Bytes
/
MathUtils.java
File metadata and controls
27 lines (24 loc) · 830 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
// ****************************************************************
// MathUtils.java
//
// Provides static mathematical utility functions.
//
// ****************************************************************
public class MathUtils {
//-------------------------------------------------------------
// Returns the factorial of the argument given
//-------------------------------------------------------------
public static int factorial(int n) throws IllegalArgumentException {
if (n < 1) {
throw new IllegalArgumentException("Number entered was below 1, factorial does not exist.");
}else if (n > 16) {
throw new IllegalArgumentException("Number entered was above 16, factorial too large for int type.");
}else if (n > 16) {
}
int fac = 1;
for (int i=n; i>0; i--) {
fac *= i;
}
return fac;
}
}