Skip to content

Commit 30ee4e3

Browse files
committed
Practice Done.
Signed-off-by: Someshdiwan <[email protected]>
1 parent f91b28f commit 30ee4e3

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
public class FactorialProgram {
2+
public static void main(String[] args) {
3+
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+
14+
}
15+
public static int factorial(int n)
16+
{
17+
if(n == 1) {
18+
System.out.println("Factorial("+n+") = 1");
19+
//Base case is 1. Factorial of 1 is 1.
20+
return 1;
21+
}
22+
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);
25+
}
26+
}
27+
}

0 commit comments

Comments
 (0)