-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExample15.java
More file actions
35 lines (26 loc) · 749 Bytes
/
Example15.java
File metadata and controls
35 lines (26 loc) · 749 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
28
29
30
31
32
33
34
35
package applications.algorithms;
/** Category: Algorithms
* ID: Example 15
* Description: Factorial calculation without recursion
* Taken From:
* Details:
* TODO
*/
public class Example15 {
public static long factorial(int n){
if(n < 0){
throw new IllegalArgumentException("Cannot calculate factorial of negative number");
}
long rslt = 1;
for (int i = 1; i <= n; ++i) {
rslt *= i;
}
return rslt;
}
public static void main(String[] args){
int n = 1;
System.out.println("Factorial of "+n+" is "+Example15.factorial(n) + "\n");
n = 10;
System.out.println("Factorial of "+n+" is "+Example15.factorial(n) + "\n");
}
}