forked from Algo-Phantoms/Algo-Tree
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactorial.java
More file actions
34 lines (28 loc) · 728 Bytes
/
factorial.java
File metadata and controls
34 lines (28 loc) · 728 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
32
33
34
/*
Factorial is the product of all positive integers less than or equal to a given positive integer
and denoted by that integer and an exclamation point.
Thus, factorial seven is written 5!, meaning 1 × 2 × 3 × 4 × 5.
Factorial zero is defined as equal to 1.
*/
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(fact(n));
}
public static int fact(int n){
if(n==0)
return 1;
return n*fact(n-1);
}
}
/*
Test Cases:
Input: 5
Output: 120
Input: 0
Output: 1
Time Complexity: O(n)
Space Complexity: O(n)
*/