-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
52 lines (49 loc) · 1.2 KB
/
Factorial.java
File metadata and controls
52 lines (49 loc) · 1.2 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import java.io.FileWriter;
import java.io.IOException;
/**
* This class demonstrates the methods of the Factorial class.
* @author Ioannis Tzeneralis
* @version 1.0
*/
class Factorial {
/**
* This is the main method.
* @param args Unused.
*/
public static void main(String args[]) {
// Repeat for every argument
for (String s: args) {
if (Long.parseLong(s)<0){
// if negative argument
System.out.println("Error: Negative Value: " + s);
}else{
System.out.println(s + "!: " + factor(Long.parseLong(s)));
}
}
try {
// Write to file named "results.txt"
FileWriter results = new FileWriter("results.txt");
for (String s: args) {
if (Long.parseLong(s)>=0){
results.write(s + "!: " + factor(Long.parseLong(s)) + "\n");
}
}
results.close();
} catch (IOException e) {
System.out.println("An error occurred while creating the file.");
e.printStackTrace();
}
}
/**
* This method returns the factor repeatedly.
* @param n The number to find factor
* @return 1
* @return n * factor(n - 1);
*/
public static long factor (long n){
if (n <= 1)
return 1;
else
return n * factor(n - 1);
}
}