Java Day 9: Recursion 3



Write a factorial function that takes a positive integer, N as a parameter and prints the result of N! (N factorial).

Note: If you fail to use recursion or fail to name your recursive function factorial or Factorial, you will get a score of 0.



  1. import java.io.*;
  2. import java.math.*;
  3. import java.security.*;
  4. import java.text.*;
  5. import java.util.*;
  6. import java.util.concurrent.*;
  7. import java.util.regex.*;
  8. public class Solution {
  9. public static void main(String[] args) {
  10. Scanner in = new Scanner(System.in);
  11. int n = in.nextInt();
  12. in.close();
  13. System.out.println(factorial(n));
  14. }
  15. private static int factorial(int n) {
  16. if (n == 1) return 1;
  17. return factorial(n - 1) * n;
  18. }
  19. }