Introducing Radical.sh

Forget Code launches a powerful code generator for building API's

Factorial of The Number Using Recursion Function in Java

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. public class JavaFactorialRecursion {
  5. public static void main(String args[]) throws NumberFormatException, IOException{
  6. System.out.println("Enter the number: ");
  7. BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  8. int a = Integer.parseInt(br.readLine());
  9. int result= fact(a);
  10. System.out.println("Factorial of the number is: " + result);
  11. }
  12. static int fact(int b)
  13. {
  14. if(b <= 1)
  15. //if the number is 1 then return 1
  16. return 1;
  17. else
  18. //else call the same function with the value - 1
  19. return b * fact(b-1);
  20. }
  21. }