- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIterativeFactorial.java
More file actions
Latest commit
23 lines (21 loc) · 800 Bytes
/
Copy pathIterativeFactorial.java
File metadata and controls
23 lines (21 loc) · 800 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Christopher Yonek
// Compute Factorial (Iteration)
importjava.util.Scanner;
publicclassIterativeFactorial {
publicstaticvoidmain(String[] args) {
System.out.print("Enter a non-negative integer: ");
ScanneruserInput = newScanner(System.in);
intn = userInput.nextInt();
longstartTime = System.nanoTime();
System.out.println("Factorial of " + n + " is " + factorial(n));
longelapsedTime = System.nanoTime() - startTime;
System.out.println(elapsedTime);
}
// Method to find factorial given n
staticlongfactorial(intn){
longfactorialAns = 1, i;
for (i = 2; i <= n; i++)
factorialAns *= i;
returnfactorialAns;
}
}