Recursion
Recursion is a programming technique where a method calls itself to solve smaller instances of a larger problem.
public class RecursionExample {
public static int factorial(int n) {
if (n == 0) {
return 1;
} else {
return n * factorial(n - 1);
}
}
public static void main(String[] args) {
int number = 5;
int result = factorial(number);
System.out.println("Factorial of " + number + " is: " + result);
}
} The recursive approach involves defining a base case to stop the recursion and a recursive step where the method calls itself.
Semantic portal