We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 9e7e182 commit 4ca1015Copy full SHA for 4ca1015
18.3_FactorialCalculator/src/FactorialCalculator.java
@@ -0,0 +1,23 @@
1
+/**
2
+ * Fig. 18.3: FactorialCalculator
3
+ * Recursive factorial method.
4
+ */
5
+public class FactorialCalculator
6
+{
7
+ // recursive method factorial (assumes its parameter is >= 0)
8
+ public long factorial (long number)
9
+ {
10
+ if (number <= 1)
11
+ return 1; // test for base case
12
+ else
13
+ return number * factorial(number - 1);
14
+ } // end method factorial
15
+
16
+ // output factorials for values 0-21
17
+ public static void main(String[] args)
18
19
+ // calculate the factorials of 0 through 21
20
+ for (int counter = 0; counter <= 21; counter++)
21
+ System.out.printf("%d! = %d\n", counter, factorial(counter) );
22
+ }
23
+}
0 commit comments