We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
1 parent f91b28f commit 30ee4e3Copy full SHA for 30ee4e3
1 file changed
Section10Methods/RecurssionPractice/src/FactorialProgram.java
@@ -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