Write a Java method to find factorial using recursion in java


Introduction

In this demo I have used NetBeans IDE 8.2 for debugging purpose. But you can use any java programming language compiler as per your availability..


techstudy.org

HP 15 Core i3 7th gen Laptop(4GB, 1TB HDD, Windows 10) | Rs. 31,490

  1. class Javaexcercise{
  2. static int factorialofnumber(int n){
  3. if (n == 0)
  4. return 1;
  5. else
  6. return(n * factorialofnumber(n-1));
  7. }
  8. public static void main(String args[]){
  9. int fact=1;
  10. int number=5 ;//It is the number to calculate factorial
  11. fact = factorialofnumber(number);
  12. System.out.println("Factorial of "+number+" is: "+fact);
  13. }
  14. }

Result

java techstudy.org

     





Related Topics




Popular Posts