-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorialProgram.java
More file actions
executable file
·64 lines (60 loc) · 1.99 KB
/
FactorialProgram.java
File metadata and controls
executable file
·64 lines (60 loc) · 1.99 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
53
54
55
56
57
58
59
60
61
62
63
64
/**
* Calculates and displays the factorial for a number from the commandline.
* @author Lisa Miller from W Albritton
* @since 1/28/24
*/
public class FactorialProgram {
/**
* Main method.
* @param args 1st argument should be a non-negative int
*/
public static void main(String[] args) {
// check for correct number of commandlineArguments (at least 1)
// always print instructions for useage here if using args.
if (args.length == 0) {
System.out.print("Error: program requires an int");
System.out.println(" >= 0 as a commandline argument.");
}
else {
// initialize variables
int number = 0;
int result = 0;
// error checking to see if first arg is an integer
try {
number = Integer.parseInt(args[0]);
// call method & display results
result = factorial(number);
// error checking to see if it is non-negative
if (result == -1) {
System.out.println("ERROR: Negative integers are undefined");
}
else {
System.out.println(number + "! = " + result);
}
}
catch (NumberFormatException exception) {
System.out.println("The first commandline argument is not an integer");
}
}
} // end of main
/**
* Computes the factorial of a nonnegative number.
* @param n is a nonnegative integer
* @return the factorial of n, or -1 for negative integers (error)
*/
public static int factorial(int n) {
int product = 1; //def of 0!
int i = n;
// check for negative integers
if (n < 0) {
product = -1; // error condition
} else {
while (i > 0) { //if i == 0 loop will not run
product *= i;
i--;
}
}
// return result of factorial
return product;
}
} // end of class