1! is equal to 4 × 3 × 2 × 1 = 24 . .
n! is equal to
n
× (n
– 1)
× (n
– 2) ×
…
× 3 × 2 × 1
An alternative way to describe the calculation of
n! is the recursive formula
n
× (n
– 1)!, plus a base case of 0!, which is 1. Write a static method that
implements this recursive formula for factorials. Place the method in a
test program that allows the user to enter values for
n
until signaling an
end to execution.
2. A common example of a recursive formula is one to compute the sum
of the first
n
integers, 1 + 2 + 3 +
…
+
n. The recursive formula can be expressed
as
1 + 2 + 3 +
…
+
n
=
n
+ (1 + 2 + 3 +
…
+ (n
– 1))
Write a static method that implements this recursive formula to compute
the sum of the first
n
integers. Place the method in a test program that
allows the user to enter the values of
n
until signaling an end to execution.
Your method definition should not use a loop to add the first
n
integers