Programming recursion is the process by which a method calls itself to resolve an issue.
The factorial of a number n (written as n!) is:
n! = n × (n - 1) × (n - 2) × ... × 1
class RecursionExample {
// Recursive method to calculate factorial
static int factorial(int n) {
if (n == 1) {
return 1; // base case
else {
return n * factorial(n - 1); // recursive call
}
}
public static void main(String[] args) {
int number = 5;
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
}
}
We use cookies to enhance your experience, to provide social media features and to analyse our traffic. By continuing to browse, you agree to our Privacy Policy.
1 Answer
Dewesh B.T . 2 weeks ago
Recursion
Programming recursion is the process by which a method calls itself to resolve an issue.
The factorial of a number n (written as n!) is: