Recursion 1 Implement the recursive factorial function given
Recursion
1. Implement the recursive factorial function given below (from the lecture notes on recursion).
Develop a main program that allows the user to enter any integer (positive) integer value, and
displays the factorial of that value. The program should allow the user to either keep entering
another value, or quit the program.
public static int factorial(int n)
{
if (n == 0)
return (1);
else
return (n * factorial(n1));
}
2. Determine the largest value for n that can be correctly computed.
3. Implement the recursive Towers of Hanoi function given below (from the lecture notes). Develop a
main program that allows the user to enter any number of disks to solve.
public static void towers(int numDisks,
String startPeg, String destPeg, String sparePeg)
{
if (numDisks == 1)
System.out.println(\"Move disk from \" + startPeg + \" to \" + destPeg);
else
{
towers(numDisks-1, startPeg, sparePeg, destPeg);
System.out.println(\"Move disk from \" + startPeg + \" to \" + destPeg);
towers(numDisks-1, sparePeg, destPeg, startPeg);
}
}
4. Modify function towers so that all the print statements are disabled (i.e., comment them out). There
needs to be a statement after if(numDisks …), so replace that with just numDisks = numDisks. (The
compiler will actually remove this pointless statement.)
Modify the main program so that “Starting towers …” is displayed right before the function is called,
and “Finished towers” when completed (right after the function call). Run your program using this
new version of the function (that does not display the moves) to determine how long it takes to
complete for the following number of disks: 10, 20, 30, 32, 34, 36. Use your smart phone to
determine each execution time to the nearest second.
For this lab, turn in all your Java source files. Enter your responses to questions 2 and 4 in the comments box when submitting.
Solution
1)
package factorialfind;
import java.util.Scanner;
public class FactorialFind {
public int factorial(int n)
{
if (n == 0)
return (1);
else
return n*factorial(n-1);
}
public static void main(String[] args) {
int value;
FactorialFind fact = new FactorialFind();
Scanner sc = new Scanner(System.in);
do{
System.out.println(\"Enter positive integer to find it Factorial\");
value = sc.nextInt();
System.out.println(\"Factorial :\"+fact.factorial(value));
}while(value > 0 );
}
}
2) 16 is the value that is computing correctly.
3)a)Code:
package factorialfind;
import java.util.Scanner;
public class FactorialFind {
public int factorial(int n)
{
if (n == 0)
return (1);
else
return n*factorial(n-1);
}
public void towers(int numDisks,String startPeg, String destPeg, String sparePeg)
{
if (numDisks == 1)
System.out.println(\"Move disk from \" + startPeg + \" to \" + destPeg);
else
{
towers(numDisks-1, startPeg, sparePeg, destPeg);
System.out.println(\"Move disk from \" + startPeg + \" to \" + destPeg);
towers(numDisks-1, sparePeg, destPeg, startPeg);
}
}
public static void main(String[] args) {
int value;
int numDisks;
String startPeg,destPeg,sparePeg;
Scanner sc = new Scanner(System.in);
FactorialFind fact = new FactorialFind();
while(true){
System.out.println(\"Enter positive integer to find it Factorial\");
value = sc.nextInt();
if(value > 0)
System.out.println(\"Factorial :\"+fact.factorial(value));
else{
System.out.println(\"Input is Negative:\ \\t Continu with Towers of Hanoi\");
break;
}
}
System.out.println(\"Enter number of Disks:\");
numDisks = sc.nextInt();
System.out.println(\"Enter start Peg:\");
startPeg = sc.next();
System.out.println(\"Enter destination Peg:\");
destPeg = sc.next();
System.out.println(\"Enter spare Peg:\");
sparePeg = sc.next();
fact.towers(numDisks, startPeg, destPeg, sparePeg);
}
}
Output:
Enter positive integer to find it Factorial
6
Factorial :720
Enter positive integer to find it Factorial
-5
Input is Negative:
Continu with Towers of Hanoi
Enter number of Disks:
3
Enter start Peg:
A
Enter destination Peg:
C
Enter spare Peg:
B
Move disk from A to C
Move disk from A to B
Move disk from C to B
Move disk from A to C
Move disk from B to A
Move disk from B to C
Move disk from A to C
BUILD SUCCESSFUL (total time: 28 seconds)


