sunfresh

Factorial

Jan 20th, 2023
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. Factorial
  2. Given n of 1 or more, return the factorial of n, which is n * (n-1) * (n-2) ... 1. Compute the result recursively (without loops).
  3.  
  4. Input
  5. On the first line you will be given n.
  6.  
  7. Output
  8. On the only output line you should print F(n).
  9.  
  10. Constraints
  11. n>1
  12.  
  13. Sample tests
  14. Input
  15. 1
  16. Output
  17. 1
  18. Input
  19. 5
  20. Output
  21. 120
  22.  
  23.  
  24. import java.util.Scanner;
  25.  
  26. public class Factoriel {
  27. public static void main(String[] args) {
  28. Scanner scanner = new Scanner(System.in);
  29. int n = Integer.parseInt(scanner.nextLine());
  30. System.out.println(factoriel(n));
  31.  
  32. }
  33. public static int factoriel(int n){
  34. if(n<=1){
  35. return 1;
  36. }
  37. return n * factoriel(n-1);
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment