-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
29 lines (27 loc) · 787 Bytes
/
Factorial.java
File metadata and controls
29 lines (27 loc) · 787 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
import java.io.*;
import java.util.Scanner;
public class Factorial
{
// factorial through iteration
public static long factorial(long N){
// start @ value of N
long Value = N;
for(int i = N; i > 0; i--){
// multiply current sum of values by N
// unless N = 0, in which case don't do it!
Value = (Value * N);
}
return Value;
}
// factorial through recursion
public static long factorial(long n){
return ((n<2) ? 1 : (n * factorial(n-1)));
}
public static void main(String [] args){
System.out.println("Factorial of: ");
Scanner s = new Scanner(System.in);
long N = s.nextLong();
long Factorial = factorialR(N);
System.out.println("Factorial of " + Long.valueOf(N) + " = " + Long.valueOf(Factorial));
}
}