Skip to content

Commit 582f3aa

Browse files
committed
feat: Add FactorialProgram with recursive factorial calculation
WHAT the code does: - Defines a `FactorialProgram` class to compute factorial of a number using recursion. - `factorial(int n)`: - **Base case**: if `n == 1`, prints `"Factorial(1) = 1"` and returns 1. - **Recursive case**: prints current formula (`n * Factorial(n-1)`) and recursively calls `factorial(n-1)`. - `main()` calls `factorial(7)` and prints the result. WHY this matters: - Demonstrates **recursion** in Java with factorial, a classic example. - Shows **base case and recursive case structure**. - Helps understand how recursive calls expand and collapse during execution. HOW it works (factorial(7)): 1. Prints: `Factorial(7) = 7 * Factorial(6)`. 2. Calls `factorial(6)` → prints `Factorial(6) = 6 * Factorial(5)`. 3. Continues until `Factorial(1) = 1`. 4. Call stack unwinds, multiplying results → `5040`. Tips & gotchas: - Factorial grows very fast — `int` will overflow beyond 12! → use `long` or `BigInteger` for larger inputs. - No check for `n <= 0` → negative or zero input leads to infinite recursion or incorrect results. - Printing inside recursion is useful for learning but should be removed in production for efficiency. Use-cases: - Educational demo for **recursion basics**. - Useful in algorithm teaching (factorials, Fibonacci, recursion patterns). - Foundation for more advanced recursion problems. - Can be adapted for iterative factorial implementations to compare performance. Short key: algo-factorial-recursion-trace. Signed-off-by: https://github.com/Someshdiwan <[email protected]>
1 parent d6285d5 commit 582f3aa

File tree

1 file changed

+13
-14
lines changed

1 file changed

+13
-14
lines changed
Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
11
public class FactorialProgram {
22
public static void main(String[] args) {
33
System.out.println(factorial(7));
4-
5-
/*
6-
Number Multiply by each Preceding number.
7-
5! = 5 * 4 * 3 * 2 * 1 = 120
8-
9-
if u called method inside itself i will repeat infinite many times.
10-
need Base case to know when to stop the method.
11-
Base case need.
12-
*/
13-
144
}
15-
public static int factorial(int n)
16-
{
5+
public static int factorial(int n) {
176
if(n == 1) {
187
System.out.println("Factorial("+n+") = 1");
198
//Base case is 1. Factorial of 1 is 1.
209
return 1;
2110
}
2211
else {
23-
System.out.println("Factorial(" + n + ") = " + n + " * Factorial(" + (n-1)+ ")"); //Printing current value and Formula version.
24-
return n * factorial(n - 1); //current number n and n * ( n - 1);
12+
System.out.println("Factorial(" + n + ") = " + n + " * Factorial(" + (n-1)+ ")");
13+
/*Printing current value and Formula version.*/
14+
15+
return n * factorial(n - 1);
16+
//current number n. so, n * ( n - 1); 5 * (5 - 1) = 20.
2517
}
2618
}
2719
}
20+
21+
/* Number Multiply by each Preceding number.
22+
5! = 5 * 4 * 3 * 2 * 1 = 120
23+
if u called method inside itself i will repeat infinite many times.
24+
need Base case to know when to stop the method.
25+
Base case need.
26+
*/

0 commit comments

Comments
 (0)