Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Recursive and iterative How can I improve the following code using both Recursive and iterative approaches ? the code is only recursive , also needs

Recursive and iterative

How can I improve the following code using both Recursive and iterative approaches ?

the code is only recursive , also needs timings how long it will take to move n number of discs

Move disk 1 from tower A to tower C Move disk 2 from tower A to tower B Move disk 1 from tower C to tower B

image text in transcribed
POT! (. . TowerOfHanoi.java 83 0 Design_a_Compiler.java TowerOfHanoi > # src > #(default package) > TowerOfHanoi > main(String) : void 1 import java. util. Scanner; public class TowerOfHanoi { 46 public TowerOfHanoi( ) { 6 // TODO Auto-generated constructor stub 70 8 public static void PermuteAndArrange(int N, char from, char aux, char to) { if (N == 1) { 10 } else { System. out. printin( "Move Disk 1 from " + from + " to " + to); 11 12 PermuteAndArrange (N - 1, from, to, aux); 13 System. out. printIn( "Move Disk " + N + " from " + from + " to " + to); 14 PermuteAndArrange (N - 1, aux, from, to); 15 16 170 public static void main (String args) { 218 19 // TODO Auto-generated method stub 20 Scanner sc = new Scanner(System. in); 21 System. out. print In("Enter the number of Disks" ); 22 int numberofDisk = sc. nextInt() ; 23 PermuteAndArrange( numberofDisk, 'A' , 'B' , 'C'); 24 25 26 27 28 Problems @ Javadoc [ Declaration 2) Console 83 No consoles to display at this time

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Programming questions