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*;
  2. import java.math.*;
  3. import*;
  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(;
  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. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]

donate a cup of tea :)

Join Our Facebook Group

Share this solution

download android app

Popular HackerRank Java Solutions is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube