Factorial with long
Completed
Read n (0–20) and print n!.
Input
One integer.
Output
n!
Example
Input
5
Output
120
Editor
Java
Hints
20! is too big for an int but fits in a long.
Solution explanation
21! overflows even a long; BigInteger (Advanced chapter) handles any size.
Show solution
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
System.out.println(result);
}
}