How to calculate factorial of a number using recursion in Java

1 Answer

0 votes
public class MyClass {
    static int recursiveFactorial(int n) {     
        if (n == 0)  {  
            return 1; 
        }
        else {
            return n * recursiveFactorial(n-1);    
        }
    }    
    
    public static void main(String args[]) {
        int number = 6;
         
        System.out.println("Factorial of " + number + " is: " + recursiveFactorial(number));    
    }
}
 
 
 
 
/*
run:
 
Factorial of 6 is: 720
 
*/


answered May 8, 2015 by avibootz
edited May 20, 2024 by avibootz

Related questions

2 answers 177 views
2 answers 261 views
1 answer 165 views
1 answer 1,002 views
3 answers 339 views
...