- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathComputeFactorial.java
More file actions
Latest commit
31 lines (20 loc) · 739 Bytes
/
Copy pathComputeFactorial.java
File metadata and controls
31 lines (20 loc) · 739 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
// Christopher Yonek
// Compute Factorial (Recursion)
importjava.util.Scanner;
publicclassComputeFactorial {
publicstaticvoidmain(String[] args) {
Scannerinput = newScanner(System.in);
System.out.print("Enter a non-negative integer: ");
intn = input.nextInt();
longstartTime = System.nanoTime();
System.out.println("Factorial of " + n + " is " + factorial(n));
longelapsedTime = System.nanoTime() - startTime;
System.out.println(elapsedTime);
}
publicstaticlongfactorial(intn) {
if (n == 0) // Base case
return1;
else
returnn * factorial(n - 1); // Recursive call
}
}