Question

In: Computer Science

1. Implement the recursive factorial function given below (from the lecture notes on recursion). Develop a...

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 || n == 1) return (1); else return (n * factorial(n-1)); } 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. use java

Solutions

Expert Solution

Factorial Program using Recursion:

class FactorialExample{  
 static int factorial(int n){    
  if (n == 0)    
    return 1;    
  else    
    return(n * factorial(n-1));    
 }    
 public static void main(String args[]){  
  int i,fact=1;  
  int number=4;//It is the number to calculate factorial    
  fact = factorial(number);   
  System.out.println("Factorial of "+number+" is: "+fact);    
 }  
}  

Towers of hanio:

import java.util.*;
class GFG 
{ 
    // Java recursive function to solve tower of hanoi puzzle 
    static void towerOfHanoi(int n, char from_rod, char to_rod, char aux_rod) 
    { 
        if (n == 1) 
        { 
            System.out.println("Move disk 1 from rod " +  from_rod + " to rod " + to_rod); 
            return; 
        } 
        towerOfHanoi(n-1, from_rod, aux_rod, to_rod); 
        System.out.println("Move disk " + n + " from rod " +  from_rod + " to rod " + to_rod); 
        towerOfHanoi(n-1, aux_rod, to_rod, from_rod); 
    } 
      
    //  Driver method 
    public static void main(String args[]) 
    { 
        int n ; // Number of disks
        Scanner sc = new Scanner(System.in); 
        System.out.println("Enter no.of disks");
        n = sc.next();
        towerOfHanoi(n, \'A\', \'C\', \'B\');  // A, B and C are names of rods 
    } 
} 

Related Solutions

THIS IS ALL ON PYTHON Recursion Practice Recall from lecture that a recursive function is one...
THIS IS ALL ON PYTHON Recursion Practice Recall from lecture that a recursive function is one that calls on itself. Recursive functions have two parts, the base case which stops the recursion and the recursive step where the function calls upon itself. When designing recursive algorithms, your goal for each step should be to work towards the base case. More abstractly, each recursive call decreases the size of the problem, working ever closer to some trivial case with a defined...
Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
Write a recursive function to calculate and return factorial of a given number 'n'. in C...
Write a recursive function to calculate and return factorial of a given number 'n'. in C progrmaining
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main...
C++ Recursion, Change the delete_node function in the header file to a recursive delete_node function, main is already set. Hint: It might be helpful to modify the function so that it uses a separate recursive function to perform whatever processing is needed. //////////////////////////////////////////////////////////////header.h////////////////////////////////////////////////////////////////////////////////////////////// #ifndef HEADER_H_ #define HEADER_H_ #include <iostream> using namespace std; template <class T> class LL { private:    struct LLnode    {        LLnode* fwdPtr;        T theData;    };    LLnode* head; public:    LL();...
There is a Java program that is missing one recursive function: public class Factorial { /*...
There is a Java program that is missing one recursive function: public class Factorial { /* / 1 when n is 0; * fact(n) = | * \ n*fact(n-1) otherwise */ public static int fact(int n) { return 0; } /* Factorial Test Framework * * Notice the odd expected value for fact(20). It is negative because * fact(20) should be 2432902008176640000, but the maximum int is only * 2147483647. What does Java do when integers run out of range?...
Recursion. Question 1 1.1 What are the 2 main components of a recursive function? 1.2 What...
Recursion. Question 1 1.1 What are the 2 main components of a recursive function? 1.2 What is more efficient: an explicit loop structure or a recursive function? Explain your answer. 1.3 In what scenarios should a recursive solution be preferred to an iterative solution?
develop a recursive function to compute LCS (x,y)
develop a recursive function to compute LCS (x,y)
One of your chapters this week is on recursion. Below are the three recursive algorithms laws:...
One of your chapters this week is on recursion. Below are the three recursive algorithms laws: 1. A recursive algorithm must have a base case. 2. A recursive algorithm must change its state and move toward the base case. 3. A recursive algorithm must call itself, recursively. Do you have any examples of recursive algorithms used in the real-world? Approximately 175 words in the answer.
Recursion is a programming technique in which a function calls itself. This project applies recursion to a new problem, and illustrates the recursive implementation of backtracking.
RECURSIVELY FINDING PATHS THROUGH A MAZE in C++INTRODUCTIONRecursion is a programming technique in which a function calls itself. This project applies recursion to a new problem, and illustrates the recursive implementation of backtracking.DESCRIPTIONConsider finding all paths from an entrance on the top of a maze to an exit on the bottom. This task can be accomplished recursively, so in this project, you design, implement, test, and document a program that calls a recursive function to find a path through a maze.An...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive...
Given the following definition of the LNode class, implement the non-recursive method saveCountinLastNode and the recursive method addOddNodes for the LinkedList class which represents singly linked lists. public class LNode { private int m_info; private LNode m_link; public LNode(int info){ m_info = info; m_link = null; } public void setLink(LNode link){ m_link = link; } public LNode getLink(){   return m_link; } public void setInfo(int info){ m_info = info; } public int getInfo(){   return m_info; } } public class LinkedList {...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT