-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEight.java
39 lines (30 loc) · 914 Bytes
/
Eight.java
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
import java.util.Scanner;
public class Eight {
// Calculate factorial using for loop
static void findFactorial(int num) {
int fact = 1;
for (int i = 1; i <= num; i++) {
fact = fact * i;
}
System.out.println("The factorial is >> " + fact);
}
// Calculate factorial using recursion
static int factorial(int num) {
if (num == 0) {
return 1;
} else {
return (num * factorial(num - 1));
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number : ");
int num = sc.nextInt();
// Call the findFactorial() function.
findFactorial(num);
// Call factorial() function.
int Fact = factorial(num);
System.out.println("Factorial is >> " + Fact);
sc.close();
}
}