-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorialExample.java
More file actions
33 lines (27 loc) · 818 Bytes
/
Copy pathFactorialExample.java
File metadata and controls
33 lines (27 loc) · 818 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
// Online Java Compiler
// Use this editor to write, compile and run your Java code online
class FactorialExample {
public static void main(String[] args) {
int n=5;
int fact = 1;
for(int i=n;i>=1;i--){
fact = i*fact;
}
// 5*4*3*2*1
System.out.println("Factorial of " + n + " is: " + fact);
}
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------
// With Recursion
public class Main {
static int factorial(int n) {
if (n > 1) {
return n * factorial(n - 1);
} else {
return 1;
}
}
public static void main(String[] args) {
System.out.println("Factorial of 5 is " + factorial(5));
}
}