Usually recursive methods demands a few lines of code but a deeply thought about your algorithm. If you make a logical mistake, you'll probably obtain a StackOverflowError.
Here follows 2 programming examples for factorial:
Iteractive:Iteractive:
public int calculateIteractiveFactorial(int n) { // Base case if (n == 1) { return 1; } int factorial = 1; for (int i = 1; i <= n; i++) { factorial = factorial * i; } return factorial; } Recursive:Recursive:
public int calculateRecursiveFactorial(int n) { // Base case if (n == 1) { return 1; } return n * calculateRecursiveFactorial(n - 1); } Hope you get the idea. It's always good to know how to thinkreflect about different solutionsideas for any ideaeach proposal, always considering lines of code, complexity, clarity, cohesion, etc.