fork download
  1. import java.util.Scanner;
  2.  
  3. class Factorial {
  4.  
  5. public static long factorial(int n) {
  6. long result = 1;
  7.  
  8. if (n < 0) {
  9. return 1;
  10. }
  11.  
  12. for (int i = 2; i <= n; i++) {
  13. result *= i;
  14. }
  15.  
  16. return result;
  17. }
  18.  
  19. public static void main(String[] args) {
  20. Scanner scanner = new Scanner(System.in);
  21. System.out.print("Enter an integer n: ");
  22.  
  23. int n = scanner.nextInt();
  24. scanner.close();
  25.  
  26. System.out.println("Output: " + factorial(n));
  27. }
  28. }
Success #stdin #stdout 0.17s 58944KB
stdin
5
stdout
Enter an integer n: Output: 120