-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFactorial.java
More file actions
40 lines (35 loc) · 1.53 KB
/
Copy pathFactorial.java
File metadata and controls
40 lines (35 loc) · 1.53 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
package Factorial;
import java.util.Scanner; // Scanner class is required to get input from user
public class Factorial {
static long number = 0; // the required number to find the factorial of that number.
static long factorialValue = 0; // the factorial value of the given number.
public static void main(String[] args) {
// if user provided commandline-arguments, then
if (args.length != 0) {
if (args.length == 1)
number = Long.parseLong(args[0]);
else
System.out.println("[!] Available arguments: [Number]");
} // else get input from user
else {
Scanner scanner = new Scanner(System.in);
System.out.print("[<-] Enter a number to find the factorial: ");
number = scanner.nextLong();
scanner.close();
}
factorialValue = generateFactorial(number);
if (factorialValue == -1)
System.out.println("[->] Factorial of " + number + " is undefined.");
else
System.out.println("[->] Factorial of " + number + " is " + factorialValue + ".");
}
// a recursive method to generate the Factorial of a number n
public static long generateFactorial(long number) {
if (number < 0) // if the number is negative then return -1
return -1;
if (number == 1 || number == 0) // if the number is 1 or 0 then return 1 or 0 respectively
return 1;
else
return number * generateFactorial(number - 1);
}
}