Modify the factorial method


Discuss the below:

Q: Modify the factorial method in Fig. below to print its local variable and recursive-call parameter. For each recursive call, display the outputs on a separate line, and add a level of indentation. Do your utmost to make the outputs clear, interesting and meaningful. Your goal here is to design and implement an output format that makes it easier to understand recursion. You may want to add such display capabilities to other recursion examples and exercises throughout the text.

Fig:

// FactorialCalculator.java
// Recursive factorial method.

public class FactorialCalculator
{
// recursive declaration of method factorial
public long factorial( long number )
{
if ( number <= 1 ) // test for base case
return 1; // base cases: 0! = 1 and 1! = 1
else // recursion step
return number * factorial( number - 1 );
} // end method factorial

// output factorials for values 0-10
public void displayFactorials()
{
// calculate the factorials of 0 through 10
for ( int counter = 0; counter <= 10; counter++ )
System.out.printf( "%d! = %dn", counter, factorial( counter ) );
} // end method displayFactorials
} // end class FactorialCalculator

Solution Preview :

Prepared by a verified Expert
JAVA Programming: Modify the factorial method
Reference No:- TGS01935362

Now Priced at $25 (50% Discount)

Recommended (90%)

Rated (4.3/5)