File tree Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Expand file tree Collapse file tree 1 file changed +27
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments