forked from dimpeshpanwar/javabasicprograms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFactorial.java
More file actions
24 lines (21 loc) · 704 Bytes
/
Factorial.java
File metadata and controls
24 lines (21 loc) · 704 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
import java.util.Scanner;
public class Factorial {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a non-negative integer to calculate its factorial: ");
int number = scanner.nextInt();
long factorial = calculateFactorial(number);
System.out.println("The factorial of " + number + " is " + factorial);
}
public static long calculateFactorial(int n) {
if (n == 0 || n == 1) {
return 1;
} else {
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
}
}