-
Notifications
You must be signed in to change notification settings - Fork 3.7k
/
Copy pathFactorial.java
35 lines (31 loc) · 1.22 KB
/
Factorial.java
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
import java.util.Scanner;
class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number to find its factorial: ");
int n = sc.nextInt();
// Try to calculate the factorial of n, catch any exceptions that are thrown.
try {
System.out.println(n + "! = " + factorial(n));
} catch (Exception e) {
System.out.println(e.getMessage());
}
sc.close();
}
/**
* This method recursively calculates the factorial of an integer.
* @param n An integer we are finding the factorial of.
* @return The factorial of `n`.
* @throws Exception If `n` is negative.
*/
public static int factorial(int n) throws Exception {
// Check that factorial is non-negative, throw an expection if not.
if (n < 0) {
throw new Exception("Factorial of negative number is not possible");
} else if (n == 0 || n == 1) { // Base cases, no need to recurse further.
return 1;
} else { // Non-negative n and not at a base case, so multiply n by the factorial of n-1 and return it.
return n * factorial(n - 1);
}
}
}